Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid remote services: no communication
    primarykey
    data
    text
    <p>I am trying to make an Activity run a certain service.</p> <p>I followed the tutorial <a href="http://saigeethamn.blogspot.com/2009/09/android-developer-tutorial-part-9.html" rel="nofollow">here</a> 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 (<code>IMyRemoteCallsLoggingService</code>) object does not seem to have the connection properly created.</p> <p>I have been trying to make this work for several days but I can't seem to get rid of a <code>NullPointException</code>.</p> <p>Not sure if I made myself clear, in which case here's the code:</p> <pre><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.CallsLoggingService"); 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"); } }; </code></pre> <p>I get a <code>NullPointerException</code> right on <code>callsLoggingService.dumpCallsLog()</code> in the <code>invokeCallsService()</code> method, and I'm not sure what's the problem!</p> <p>Here's the code of the service:</p> <pre><code>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()); } } </code></pre> <p>}</p> <p>Thanks a lot everybody for the help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload