Getting data from Service?

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

Getting data from Service?

Postby Draffodx » Tue Jan 19, 2010 1:42 pm

have a service that 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
the service to 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 my service class, 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.

--------------------

Service class

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class WIFIService extends Service{
  2.  
  3.  
  4.  
  5.    private static final int WIFI_NOTIFY = 0x2001;
  6.  
  7.    public static final String EXTRA_UPDATE_RATE = "update-rate";
  8.  
  9.    public static final String WIFI_SERVICE =
  10.  
  11. "cicero.org.WIFIService.SERVICE";
  12.  
  13.  
  14.  
  15.        private WifiManager mainWifi;
  16.  
  17.        private BroadcastReceiver rssiListener = null;
  18.  
  19.  
  20.  
  21.        private int updateRate = -1;
  22.  
  23.        private int connectedLevel;
  24.  
  25.  
  26.  
  27.  
  28.  
  29.        @Override
  30.  
  31.        public void onCreate(){
  32.  
  33.                super.onCreate();
  34.  
  35.                mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
  36.  
  37.                notifier = (NotificationManager) getSystemService
  38.  
  39. (Context.NOTIFICATION_SERVICE);
  40.  
  41.  
  42.  
  43.        }
  44.  
  45.  
  46.  
  47.        @Override
  48.  
  49.        public void onStart(Intent intent, int startId){
  50.  
  51.                super.onStart(intent, startId);
  52.  
  53.  
  54.  
  55.                updateRate = intent.getIntExtra(EXTRA_UPDATE_RATE, -1);
  56.  
  57.                if(updateRate == -1){
  58.  
  59.  
  60.  
  61.                        updateRate = 60000;
  62.  
  63.  
  64.  
  65.                }
  66.  
  67.                 rssiListener = new BroadcastReceiver(){
  68.  
  69.  
  70.  
  71.             @Override
  72.  
  73.             public void onReceive(Context context, Intent intent) {
  74.  
  75.                     String action = intent.getAction();
  76.  
  77.  
  78.  
  79.                     if(WifiManager.RSSI_CHANGED_ACTION.equals(action))
  80.  
  81. {
  82.  
  83.                        WifiInfo data = mainWifi.getConnectionInfo();
  84.  
  85.                        Log.d("WIFI SERVICE", "RSSI has changed");
  86.  
  87.                        if(mainWifi.getConnectionInfo()!=null){
  88.  
  89.                                setConnectedLevel(data.getRssi());
  90.  
  91.                                Log.d("WIFI SERVICE", "new RSSI = " +
  92.  
  93. data.getSSID()+ " " + data.getRssi() + "dBm");
  94.  
  95.                        }
  96.  
  97.                     }
  98.  
  99.             }
  100.  
  101.     };
  102.  
  103.  
  104.  
  105. }
  106.  
  107.  
  108.  
  109.        @Override
  110.  
  111.        public void onDestroy(){
  112.  
  113.  
  114.  
  115.                if(rssiListener != null){
  116.  
  117.                        unregisterReceiver(rssiListener);
  118.  
  119.                        rssiListener = null;
  120.  
  121.                }
  122.  
  123.  
  124.  
  125.                if(wifiChangeListener != null){
  126.  
  127.                        unregisterReceiver(wifiChangeListener);
  128.  
  129.                        wifiChangeListener = null;
  130.  
  131.                }
  132.  
  133.  
  134.  
  135.                if(receiverWifi != null){
  136.  
  137.                        unregisterReceiver(receiverWifi);
  138.  
  139.                        receiverWifi = null;
  140.  
  141.                }
  142.  
  143.                super.onDestroy();
  144.  
  145.  
  146.  
  147.        }
  148.  
  149.                public void setConnectedLevel(int connectedLevel) {
  150.  
  151.                        this.connectedLevel = connectedLevel;
  152.  
  153.  
  154.  
  155.                }
  156.  
  157.  
  158.  
  159.  
  160.  
  161.                @Override
  162.  
  163.            public IBinder onBind(Intent intent) {
  164.  
  165.                // we only have one, so no need to check the intent
  166.  
  167.                return mRemoteInterfaceBinder;
  168.  
  169.            }
  170.  
  171.  
  172.  
  173.            //  remote interface
  174.  
  175.            private final IRemoteInterface.Stub mRemoteInterfaceBinder = new
  176.  
  177. IRemoteInterface.Stub() {
  178.  
  179.  
  180.  
  181.                        @Override
  182.  
  183.                        public int getConnectedLevel() throws RemoteException {
  184.  
  185.                                Log.v("interface", "getConnectedLevel() called");
  186.  
  187.                                return connectedLevel;
  188.  
  189.                        }
  190.  
  191.  
  192.  
  193.            };
  194.  
  195. }
  196.  
  197.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


--------------------

Activity class

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class TestApp extends Activity implements ServiceConnection{
  2.  
  3.  
  4.  
  5.        IRemoteInterface mRemoteInterface = null;
  6.  
  7.        int connectedLevel;
  8.  
  9.  
  10.  
  11.    /** Called when the activity is first created. */
  12.  
  13.    @Override
  14.  
  15.    public void onCreate(Bundle savedInstanceState) {
  16.  
  17.        super.onCreate(savedInstanceState);
  18.  
  19.        setContentView(R.layout.main);
  20.  
  21.  
  22.  
  23.        CallMonitor cm = new CallMonitor(this);
  24.  
  25.        wifi = new WiFi(this);
  26.  
  27.  
  28.  
  29.        final Button settings_Button = (Button) findViewById
  30.  
  31. (R.id.settingsButton);
  32.  
  33.  
  34.  
  35.                settings_Button.setOnClickListener(new View.OnClickListener()
  36.  
  37. {
  38.  
  39.                        public void onClick(View v){
  40.  
  41.                                final Intent myIntent = new Intent(TestApp.this,
  42.  
  43. SettingsApp.class);
  44.  
  45.                                startActivity(myIntent);
  46.  
  47.  
  48.  
  49.                                //Toast.makeText(GetCallLog.this, nameE, Toast.LENGTH_LONG).show
  50.  
  51. ();
  52.  
  53.                        }
  54.  
  55.  
  56.  
  57.                });
  58.  
  59.  
  60.  
  61.        final Button scan_Button = (Button) findViewById
  62.  
  63. (R.id.scanButton);
  64.  
  65.  
  66.  
  67.                scan_Button.setOnClickListener(new View.OnClickListener(){
  68.  
  69.                        public void onClick(View v){
  70.  
  71.  
  72.  
  73.                                Intent service = new Intent(WIFIService.WIFI_SERVICE);
  74.  
  75.                service.putExtra(WIFIService.EXTRA_UPDATE_RATE, 5000);
  76.  
  77.                startService(service);
  78.  
  79.                getConnectedData();
  80.  
  81.  
  82.  
  83.        });
  84.  
  85.  
  86.  
  87.                final Button stop_Scan_Button = (Button) findViewById
  88.  
  89. (R.id.stopScanButton);
  90.  
  91.  
  92.  
  93.                stop_Scan_Button.setOnClickListener(new View.OnClickListener
  94.  
  95. (){
  96.  
  97.                        public void onClick(View v){
  98.  
  99.  
  100.  
  101.                                Intent service = new Intent(WIFIService.WIFI_SERVICE);
  102.  
  103.                stopService(service);
  104.  
  105.  
  106.  
  107.                                }
  108.  
  109.  
  110.  
  111.                });
  112.  
  113.  
  114.  
  115.    }
  116.  
  117.  
  118.  
  119.  
  120.  
  121.    public void getConnectedData() {
  122.  
  123.  
  124.  
  125.        try {
  126.  
  127.  
  128.  
  129.            int connectedLevel = mRemoteInterface.getConnectedLevel();
  130.  
  131.  
  132.  
  133.            Log.d("GOT IT!", "connectedLevel = " + connectedLevel);
  134.  
  135.  
  136.  
  137.        } catch (RemoteException e) {
  138.  
  139.            Log.e("ServiceControl", "Call to remote interface
  140.  
  141. failed.", e);
  142.  
  143.        }
  144.  
  145.    }
  146.  
  147.  
  148.  
  149.    @Override
  150.  
  151.    protected void onResume() {
  152.  
  153.        super.onResume();
  154.  
  155.        // get a link to our remote service
  156.  
  157.        bindService(new Intent(IRemoteInterface.class.getName()),
  158.  
  159. this, Context.BIND_AUTO_CREATE);
  160.  
  161.    }
  162.  
  163.  
  164.  
  165.    @Override
  166.  
  167.    protected void onPause() {
  168.  
  169.        // remove the link to the remote service
  170.  
  171.        unbindService(this);
  172.  
  173.        super.onPause();
  174.  
  175.    }
  176.  
  177.  
  178.  
  179.        public void onServiceConnected(ComponentName className, IBinder
  180.  
  181. service) {
  182.  
  183.  
  184.  
  185.                mRemoteInterface = IRemoteInterface.Stub.asInterface(service);
  186.  
  187.                 Log.d( "SERVICE" ,"onServiceConnected" );
  188.  
  189.        }
  190.  
  191.        @Override
  192.  
  193.        public void onServiceDisconnected(ComponentName arg0) {
  194.  
  195.  
  196.  
  197.                mRemoteInterface = null;
  198.  
  199.  
  200.  
  201.        }
  202.  
  203. }
  204.  
  205.  
Parsed in 0.042 seconds, using GeSHi 1.0.8.4


Can anybody spot a mistake or where I'm missing something?
Draffodx
Master Developer
Master Developer
 
Posts: 205
Joined: Wed Nov 12, 2008 2:31 pm

Top

Postby Draffodx » Wed Jan 20, 2010 11:17 am

For anyone that comes across this I was simply not properly binding the service to my activity

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent i = new Intent();
  2.  
  3. i.setClassName("cicero.org", "cicero.org.WIFIService");
  4.  
  5. bindService(i, this, Context.BIND_AUTO_CREATE);
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
Draffodx
Master Developer
Master Developer
 
Posts: 205
Joined: Wed Nov 12, 2008 2:31 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 17 guests