开发者

Android remote services: no communication

开发者 https://www.devze.com 2023-03-21 04:06 出处:网络
I am trying to make an Activity run a certain service. I followed the tutorial here but adapted to my code, and I can\'t make it work, because when I am invoking the service after starting and bindin

I am trying to make an Activity run a certain service.

I followed the tutorial here but adapted to my code, and I can't make it work, because when I am invoking the service after starting and binding it to the activity, my Interface (IMyRemoteCallsLoggingService) object does not seem to have the connection properly created.

I have been trying to make this work for several days but I can't seem to get rid of a NullPointException.

Not sure if I made myself clear, in which case here's the code:

public class MtprojectActivity extends Activity {
[...]
private boolean started = false;

private RemoteSmsLoggingServiceConnection SmsLoggingConn = null;
private RemoteCallsLoggingServiceConnection CallsLoggingConn = null;

private IMyRemoteCallsLoggingService callsLoggingService;
private IMyRemoteSmsLoggingService smsLoggingService;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    retrievePreferences();

    Button prefBtn = (Button) findViewById(R.id.prefsBtn);

    prefBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Explicit intent to call the preferences
            Intent preferencesActivity = new Intent(getBaseContext(),
                    Preferences.class);
            startActivity(preferencesActivity);
        }
    });
}

private void retrievePreferences() {
    // Get the xml/preferences.xml preferences
    SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());
    callsCheckbox = prefs.getBoolean("callsLogChk", false);
    smsCheckbox = prefs.getBoolean("smsLogChk", false);
    locationCheckbox = prefs.getBoolean("locationLogChk", false);

    if (callsCheckbox) {
        startCallsService();
        bindCallsService();
        try {
            invokeCallsService();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    } else {

    }
    private void startCallsService() {
    if (started) {
        Toast.makeText(MtprojectActivity.this, "Service already started",
                Toast.LENGTH_SHORT).show();
    } else {
        Intent i = new Intent();
        i.setClassName("app.mtproject", "app.mtproject.开发者_如何学CCallsLoggingService");
        startService(i);
        started = true;
        updateCallsServiceStatus();
        Log.d(getClass().getSimpleName(), "startService()");
    }
}
    private void bindCallsService() {
    if (CallsLoggingConn == null) {
        CallsLoggingConn = new RemoteCallsLoggingServiceConnection();
        Intent i = new Intent();
        i.setClassName("app.mtproject", "app.mtproject.CallsLoggingService");
        bindService(i, CallsLoggingConn, Context.BIND_AUTO_CREATE);
        updateCallsServiceStatus();
        Log.d(getClass().getSimpleName(), "bindService()");
    } else {
        Toast.makeText(MtprojectActivity.this,
                "Cannot bind - service already bound", Toast.LENGTH_SHORT)
                .show();
    }
}
    private void invokeCallsService() throws RemoteException {
    if (CallsLoggingConn == null) {
        Toast.makeText(MtprojectActivity.this,
                "Cannot invoke - service not bound", Toast.LENGTH_SHORT)
                .show();
    } else {
        callsLoggingService.dumpCallsLog();
        TextView t = (TextView) findViewById(R.id.notApplicable);
        t.setText("It worked!");
        Log.d(getClass().getSimpleName(), "invokeService()");
    }
}
    class RemoteCallsLoggingServiceConnection implements ServiceConnection {
    public void onServiceConnected(ComponentName className,
            IBinder boundService) {
        callsLoggingService = IMyRemoteCallsLoggingService.Stub
                .asInterface((IBinder) boundService);
        Log.d(getClass().getSimpleName(), "onServiceConnected()");
    }

    public void onServiceDisconnected(ComponentName className) {
        callsLoggingService = null;
        updateCallsServiceStatus();
        Log.d(getClass().getSimpleName(), "onServiceDisconnected");
    }
};

I get a NullPointerException right on callsLoggingService.dumpCallsLog() in the invokeCallsService() method, and I'm not sure what's the problem!

Here's the code of the service:

public class CallsLoggingService extends Service {
String date, duration, type;

private Handler serviceHandler;
private Task myTask = new Task();

@Override
public IBinder onBind(Intent arg0) {
    Log.d(getClass().getSimpleName(), "onBind()");
    return myRemoteCallsServiceStub;
}

private IMyRemoteCallsLoggingService.Stub myRemoteCallsServiceStub = new IMyRemoteCallsLoggingService.Stub() {
    public void dumpCallsLog() throws RemoteException {
        CallsLoggingService.this.dumpCallsLog();
    }
};

@Override
public void onCreate() {
    super.onCreate();
    Log.d(getClass().getSimpleName(), "onCreate()");
}

@Override
public void onDestroy() {
    super.onDestroy();
    serviceHandler.removeCallbacks(myTask);
    serviceHandler = null;
    Log.d(getClass().getSimpleName(), "onDestroy()");
}

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    serviceHandler = new Handler();
    serviceHandler.postDelayed(myTask, 10L);
    Log.d(getClass().getSimpleName(), "onStart()");
}

class Task implements Runnable {
    public void run() {
        try {
            myRemoteCallsServiceStub.dumpCallsLog();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        serviceHandler.postDelayed(this, 86400000L);
        Log.i(getClass().getSimpleName(), "Calling the dumpCallsLog");
    }
}

private synchronized void dumpCallsLog() {
    ContentResolver cr = getContentResolver();
    String columns[] = new String[] { CallLog.Calls.DATE,
            CallLog.Calls.DURATION, CallLog.Calls.TYPE };
    Uri mContacts = CallLog.Calls.CONTENT_URI;
    Cursor c = cr.query(mContacts, columns, // Which columns to return
            null, // WHERE clause; which rows to return(all rows)
            null, // WHERE clause selection arguments (none)
            CallLog.Calls.DEFAULT_SORT_ORDER // Order-by clause
            // (ascending
            // by name)

            );
    if (c.moveToFirst()) {
        do {
            // Get the field values
            date = c.getString(c.getColumnIndex(CallLog.Calls.DATE));
            duration = c
                    .getString(c.getColumnIndex(CallLog.Calls.DURATION));
            type = c.getString(c.getColumnIndex(CallLog.Calls.TYPE));
        } while (c.moveToNext());
    }
}

}

Thanks a lot everybody for the help!


bindService() is asynchronous. You cannot use callsLoggingService until onServiceConnected() is called.

0

精彩评论

暂无评论...
验证码 换一张
取 消