I 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
public class WIFIService extends Service{
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("WIFI SERVICE", "RSSI has changed");
if(mainWifi.getConnectionInfo()!=null){
setConnectedLevel(data.getRssi());
Log.d("WIFI SERVICE", "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){
Intent service = 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){
Intent service = 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 remote service
bindService(new Intent(IRemoteInterface.class.getName()),
this, Context.BIND_AUTO_CREATE);
}
@Override
protected void onPause() {
// remove the link to the remote service
unbindService(this);
super.onPause();
}
public void onServiceConnected(ComponentName className, IBinder
service) {
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]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en