Hi Donal,


Thanks for showing this. I wound up using a similar technique by setting a
BroadcastReceiver on both the Activity and the Service and then using
sendBroadcast to send Intents back and forth between them. But the two are
otherwise not explicitly bound.



I want the Service to remain active even if/when the Activity that spaned it
goes dormant with onPause(). According to the docs with the bindService call
"…if this Context is an Activity that is stopped, the service will not be
required to continue running until the Activity is resumed."



What do you think?


On Mon, Jan 25, 2010 at 4:27 AM, Donal Rafferty <[email protected]> wrote:

> Hi WoodManEXP,
>
> It turned out in the end I just hadn't binded the service properly to the
> activity
>
> Like this:
>
>  Intent i = new Intent();
>         i.setClassName("team.org", "team.org.WIFIService");
>         bindService(i, this, Context.BIND_AUTO_CREATE);
>
>
> On Sun, Jan 24, 2010 at 10:51 PM, WoodManEXP <[email protected]> wrote:
>
>> Daffo0,
>>
>> I would like to know how you solve this because I have a similar need
>> with a background service doing a job and the background service would
>> like to push updates to an activity (if the activity is running).
>>
>> What are the preferred communication techniques between Service and
>> Activities? You are using IRemoteInterface class. What about IBinder?
>> Have you looked at that?
>>
>>
>> On Jan 19, 12:44 pm, "[email protected]" <[email protected]> wrote:
>> > I have aservicethat is running fine and I can see the data
>> > collecting in the logcat,
>> >
>> > However after creating an aidl and trying to get the information from
>> > theserviceto my main activity I have hit a problem.
>> >
>> > I just cant seem to get the data to pass across at all, I always get a
>> > null pointer exception.
>> >
>> > Here is myserviceclass, I want to get the connectedLevel int to use
>> > in my activity class.
>> >
>> > Is it because I'm not binding properly or what am I missing but any
>> > calls to the remote interface to try and get the data ends up with a
>> > null pointer exception.
>> >
>> > My activity class code is posted underneath.
>> >
>> > --------------------
>> >
>> > Serviceclass
>> >
>> > public class WIFIService extendsService{
>> >
>> >     private static final int WIFI_NOTIFY = 0x2001;
>> >     public static final String EXTRA_UPDATE_RATE = "update-rate";
>> >     public static final String WIFI_SERVICE =
>> > "cicero.org.WIFIService.SERVICE";
>> >
>> >         private WifiManager mainWifi;
>> >         private BroadcastReceiver rssiListener = null;
>> >
>> >         private int updateRate = -1;
>> >         private int connectedLevel;
>> >
>> >         @Override
>> >         public void onCreate(){
>> >                 super.onCreate();
>> >                 mainWifi = (WifiManager)
>> getSystemService(Context.WIFI_SERVICE);
>> >                 notifier = (NotificationManager) getSystemService
>> > (Context.NOTIFICATION_SERVICE);
>> >
>> >         }
>> >
>> >         @Override
>> >         public void onStart(Intent intent, int startId){
>> >                 super.onStart(intent, startId);
>> >
>> >                 updateRate = intent.getIntExtra(EXTRA_UPDATE_RATE, -1);
>> >                 if(updateRate == -1){
>> >
>> >                         updateRate = 60000;
>> >
>> >                 }
>> >                  rssiListener = new BroadcastReceiver(){
>> >
>> >              @Override
>> >              public void onReceive(Context context, Intent intent) {
>> >                      String action = intent.getAction();
>> >
>> >                      if(WifiManager.RSSI_CHANGED_ACTION.equals(action))
>> > {
>> >                         WifiInfo data = mainWifi.getConnectionInfo();
>> >                         Log.d("WIFISERVICE", "RSSI has changed");
>> >                         if(mainWifi.getConnectionInfo()!=null){
>> >                                 setConnectedLevel(data.getRssi());
>> >                                 Log.d("WIFISERVICE", "new RSSI = " +
>> > data.getSSID()+ " " + data.getRssi() + "dBm");
>> >                         }
>> >                      }
>> >              }
>> >      };
>> >
>> > }
>> >
>> >         @Override
>> >         public void onDestroy(){
>> >
>> >                 if(rssiListener != null){
>> >                         unregisterReceiver(rssiListener);
>> >                         rssiListener = null;
>> >                 }
>> >
>> >                 if(wifiChangeListener != null){
>> >                         unregisterReceiver(wifiChangeListener);
>> >                         wifiChangeListener = null;
>> >                 }
>> >
>> >                 if(receiverWifi != null){
>> >                         unregisterReceiver(receiverWifi);
>> >                         receiverWifi = null;
>> >                 }
>> >                 super.onDestroy();
>> >
>> >         }
>> >                 public void setConnectedLevel(int connectedLevel) {
>> >                         this.connectedLevel = connectedLevel;
>> >
>> >                 }
>> >
>> >                 @Override
>> >             public IBinder onBind(Intent intent) {
>> >                 // we only have one, so no need to check the intent
>> >                 return mRemoteInterfaceBinder;
>> >             }
>> >
>> >             //  remote interface
>> >             private final IRemoteInterface.Stub mRemoteInterfaceBinder =
>> new
>> > IRemoteInterface.Stub() {
>> >
>> >                         @Override
>> >                         public int getConnectedLevel() throws
>> RemoteException {
>> >                                 Log.v("interface", "getConnectedLevel()
>> called");
>> >                                 return connectedLevel;
>> >                         }
>> >
>> >             };
>> >
>> > }
>> >
>> > --------------------
>> >
>> > Activity class
>> >
>> > public class TestApp extends Activity implements ServiceConnection{
>> >
>> >         IRemoteInterface mRemoteInterface = null;
>> >         int connectedLevel;
>> >
>> >     /** Called when the activity is first created. */
>> >     @Override
>> >     public void onCreate(Bundle savedInstanceState) {
>> >         super.onCreate(savedInstanceState);
>> >         setContentView(R.layout.main);
>> >
>> >         CallMonitor cm = new CallMonitor(this);
>> >         wifi = new WiFi(this);
>> >
>> >         final Button settings_Button = (Button) findViewById
>> > (R.id.settingsButton);
>> >
>> >                 settings_Button.setOnClickListener(new
>> View.OnClickListener()
>> > {
>> >                         public void onClick(View v){
>> >                                 final Intent myIntent = new
>> Intent(TestApp.this,
>> > SettingsApp.class);
>> >                                 startActivity(myIntent);
>> >
>> >                                 //Toast.makeText(GetCallLog.this, nameE,
>> Toast.LENGTH_LONG).show
>> > ();
>> >                         }
>> >
>> >                 });
>> >
>> >         final Button scan_Button = (Button) findViewById
>> > (R.id.scanButton);
>> >
>> >                 scan_Button.setOnClickListener(new
>> View.OnClickListener(){
>> >                         public void onClick(View v){
>> >
>> >                                 Intentservice= new
>> Intent(WIFIService.WIFI_SERVICE);
>> >                service.putExtra(WIFIService.EXTRA_UPDATE_RATE, 5000);
>> >                 startService(service);
>> >                 getConnectedData();
>> >
>> >         });
>> >
>> >                 final Button stop_Scan_Button = (Button) findViewById
>> > (R.id.stopScanButton);
>> >
>> >                 stop_Scan_Button.setOnClickListener(new
>> View.OnClickListener
>> > (){
>> >                         public void onClick(View v){
>> >
>> >                                 Intentservice= new
>> Intent(WIFIService.WIFI_SERVICE);
>> >                 stopService(service);
>> >
>> >                                 }
>> >
>> >                 });
>> >
>> >     }
>> >
>> >     public void getConnectedData() {
>> >
>> >         try {
>> >
>> >             int connectedLevel = mRemoteInterface.getConnectedLevel();
>> >
>> >             Log.d("GOT IT!", "connectedLevel = " + connectedLevel);
>> >
>> >         } catch (RemoteException e) {
>> >             Log.e("ServiceControl", "Call to remote interface
>> > failed.", e);
>> >         }
>> >     }
>> >
>> >     @Override
>> >     protected void onResume() {
>> >         super.onResume();
>> >         // get a link to our remoteservice
>> >         bindService(new Intent(IRemoteInterface.class.getName()),
>> > this, Context.BIND_AUTO_CREATE);
>> >     }
>> >
>> >     @Override
>> >     protected void onPause() {
>> >         // remove the link to the remoteservice
>> >         unbindService(this);
>> >         super.onPause();
>> >     }
>> >
>> >         public void onServiceConnected(ComponentName className,
>> IBinderservice) {
>> >
>> >                 mRemoteInterface =
>> IRemoteInterface.Stub.asInterface(service);
>> >                  Log.d( "SERVICE" ,"onServiceConnected" );
>> >         }
>> >         @Override
>> >         public void onServiceDisconnected(ComponentName arg0) {
>> >
>> >                 mRemoteInterface = null;
>> >
>> >         }
>> >
>> > }
>> >
>> > Can anybody spot a mistake or where I'm missing something?
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Android Developers" group.
>> To post to this group, send email to [email protected]
>> To unsubscribe from this group, send email to
>> [email protected]<android-developers%[email protected]>
>> For more options, visit this group at
>> http://groups.google.com/group/android-developers?hl=en
>>
>
>  --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]<android-developers%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to