Cant access service elements from activity

Put your problem here if it does not fit any of the other categories.

Cant access service elements from activity

Postby vinz89 » Thu Jun 24, 2010 11:42 pm

Hi there, i need urgent help
As I have seen you seem to be most experienced person. I need your urgent help since I am stuck here for almost past 3 days. Hope you could help me out. I would be really thankful.

My problem is that I am using a method from aidl service. The service is started and binded successfully.
Now when I use try catch from a OnClickListener Stub it works perfectly, however if I try to implement it as a code part, it gives force close error.

Also force close error goes off when I try to use another catch for RuntimeException, but then I gives blank and I null when I try e.getMessage();

My value filling code is
Code: Select all
private void fill_vals() {
      String tets = "";
      try {
                  tets = mService.ServiceStatus();
            } catch (RemoteException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                  tets = e.getMessage();
            }
    }

When I try to access fill_vals() from directly from onCreate() or other process, it gives
force close.

However when I try to access that from SetOnClickListener it works smoothly.
Code: Select all
refresh_button.setOnClickListener(new View.OnClickListener() {
                 
                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub
                        fill_vals();
                  }
            });


when I try

The following code works but canNOT access methods
Code: Select all
private void fill_vals() {
      String tets = "";
      try {
                  tets = mService.ServiceStatus();
            } catch (RemoteException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                  tets = e.getMessage();
            } catch (RuntimeException e)
            {
                  tets = e.getLocalizedMessage()+e.getMessage();
            }
            tView1.setText(tets+"ok");
    }



Coding Main Activity
Code: Select all
public class Launcher extends Activity {
      private IMainService mService = null;
      TextView tView1 = null;
      String testvar1 = "";
      /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.status_display);
        tView1 = (TextView)findViewById(R.id.feed_service_status);
       
        bindService(new Intent(IMainService.class.getName()), serCon, Context.BIND_AUTO_CREATE);
            Button refresh_button = (Button)findViewById(R.id.refresh_button);
            refresh_button.setOnClickListener(new View.OnClickListener() {
                 
                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub
                        fill_vals(); ///////////////////---HERE I CAN GET Smoothly
                  }
            });
            //fill_vals();   ///////////////////---THIS LINE CAUSE ERROR> CANT ACCESSS DIRECTLY
    }
   
    private void fill_vals() {
      String tets = "";
      try {
                  tets = mService.ServiceStatus();
            } catch (RemoteException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                  tets = e.getMessage();
            }
    }
   
    public ServiceConnection serCon = new ServiceConnection() {
           
            @Override
            public void onServiceDisconnected(ComponentName name) {
                  // TODO Auto-generated method stub
            }
           
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                  // TODO Auto-generated method stub
                  mService = MainServiceImpl.asInterface(service);

            }
      };
   
}



Code Service
Code: Select all
public class MainService extends Service {
      private boolean service_status= false;
      private boolean server_connection_status = false;
      private NotificationManager notMgr;
      public class MainServiceImpl extends IMainService.Stub {

            @Override
            public String ServiceStatus() throws RemoteException {
                  // TODO Auto-generated method stub
                  return "india";
            }

            @Override
            public boolean ServerConnectionServer() throws RemoteException {
                  // TODO Auto-generated method stub
                  return MainService.this.server_connection_status;
            }
           
      }
     
      @Override
      public void onCreate() {
            super.onCreate();
            notMgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            displayNotificationMessage("onstart create");
      }
     
      public void onDestroy() {
            super.onDestroy();
            this.service_status = false;
            displayNotificationMessage("onDestm create");
      }
     
      @Override
      public void onStart(Intent intent, int startId) {
            super.onStart(intent, startId);
            this.service_status = true;
            displayNotificationMessage("onstart started");
      }
      @Override
      public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            displayNotificationMessage("onBind started");
            return new MainServiceImpl();
      }
     
      private void displayNotificationMessage(String message) {
            // TODO Auto-generated method stub
            Random rand = new Random();
            Notification noti = new Notification(R.drawable.icon,message,System.currentTimeMillis());
            PendingIntent pIntent = PendingIntent.getActivity(this, rand.nextInt(25), new Intent(this, Launcher.class), 0);
            noti.setLatestEventInfo(this, "testServ IMPl", message, pIntent);
            notMgr.notify(rand.nextInt(),noti);
           
      }
     
}


Aidl Interface
Code: Select all
package android.vineetyadav.com.c2i;

interface IMainService {
    String ServiceStatus();
    boolean ServerConnectionServer();
}


Thanks in advance.
vinz89
Freshman
Freshman
 
Posts: 2
Joined: Sun Jun 06, 2010 8:59 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 13 guests