I have written an android service, that I need to run in the background and be able to communicate with the rest of the application. The code is as given below. The problem is that, when the service is invoked, the application force closes. And apparently it isnt reaching the service itself as none of the log commands I have written in onCreate, onStart etc is being printed
package com.org.EasyUpload;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.Application;
import android.app.DownloadManager;
import android.app.NotificationManager;
import android.app.Service;
import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
public class DownloadSnifferActivity extends Service {
public static DownloadManager MAIN_ACTIVITY;
private Timer timer = new Timer();
private static long SNIFF_INTERVAL = 1000;
private static long DELAY_INTERVAL = 1000;
SharedPreferences preferenceManager = PreferenceManager.getDefaultSharedPreferences(this);
DownloadManager downloadManager;
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("service","Control is in the onStartCommand");
// /_startService();
return START_STICKY;
}
public IBinder onBind(Intent intent) {
//return mBinder;
return null;
}
public void onCreate () {
super.onCreate();
Log.d("onCreate", "Inside onCreate");
_startService();
}
public void onDestroy (){
super.onDestroy();
_shutdownService();
}
public void onDownloadStart(String url, String userAgent, String contentDisposition,
String mimeType, long size) {
Log.d("URL",url);
}
public void _startService(){
Log.d("DownloadSniffer", "Inside the thread");
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
readDownloadManager();
Log.d("Thread", "The download sniffer service is running");
Thread.sleep(SNIFF_INTERVAL);
}catch(InterruptedException ex) {
Log.d("Exception",ex.toString());
ex.printStackTrace();
}
}
}, DELAY_INTERVAL, SNIFF_INTERVAL);
}
public void _shutdownService() {
}
public void readDownloadManager() {
DownloadManager.Query query = null;
DownloadManager downloadManager = null;
Cursor c = null;
int statusColumnIndex = 0 ;
int urlColumnIndex = 0;
long downloadProcessIdColumnNo ;
try {
query = new DownloadManager.Query();
downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
//Just for testing I initiated my own download from this url. When an http
// reuest for this url is made, since download is taking place, it gets saved in
// the download manager.
Request request = new Request(Uri.parse("http://ocw.mit.edu/courses" +
"/aeronautics-and-astronautics/16-100-aerodynamics-fall-2005" +
"/lecture-notes/16100lectre1_kvm.pdf"));
if(downloadManager!=null){
downloadManager.enqueue(request);
} else {
return;
}
if(query!=null) {
query.setFilterByStatus(DownloadManager.STATUS_PENDING);
} else {
return;
}
c = downloadManager.query(query);
Log.d("Inside", "Atleast Inside");
Log.d("Query",c.toString());
if(true){
statusColumnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
urlColumnIndex = c.getColumnIndex(DownloadManager.COLUMN_URI);
downloadProcessIdColumnNo = c.getColumnIndex(DownloadManager.COLUMN_ID);
Log.d("Column Count", ((Integer)c.getCount()).toString());
c.moveToFirst();
if(c.getCount() > 0){
String url="";
if(!c.isAfterLast()){
url = c.getString(urlColumnIndex);
downloadManager.remove(downloadProcessIdColumnNo);
Log.d("Count after remove", ((Integer)c.getCount()).toString());
c.moveToNext();
}
Log.d("After", "Stopped Working");
Log.d("url:", url);
//Here I am sending the url to another activity, where I can work with it.
Intent intent = new Intent(DownloadSnifferActivity.this, EasyUploadActivity.class);
Bundle b = new Bundle();
b.putString("url", url);
intent.putExtras(b);
startActivity(intent);
} else {
return;
}
}
} catch (NullPointerException ex) {
ex.printStackTrace();
} catch (IllegalStateException ex) {
ex.printStackTrace();
}
}
}
I have also made the following entry in AndroidManifest.xml :
<service android:enabled="true" android:name=".DownloadSnifferActivity">
</service>
And this is how I make the call to the service to start it:
startService(new Intent(EasyUploadMainMenu.this, DownloadSnifferActivity.class));
Exception logged in LogCat:
07-28 05:25:06.447: ERROR/AndroidRuntime(348): FATAL EXCEPTION: main
07-28 05:25:06.447: ERROR/AndroidRuntime(348): java.lang.RuntimeException: Unable to instantiate service com.org.EasyUpload.DownloadSnifferActivity: java.lang.NullPointerException
07-28 05:25:06.447: ERROR/AndroidRuntime(348): at android.app.ActivityThread.handleCreateService(ActivityThread.java:1904)
07-28 05:25:06.447: ERROR/AndroidRuntime(348): at android.app.ActivityThread.access$2500(ActivityThread.java:117)
07-28 05:25:06.447: ERROR/AndroidRuntime(348): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:982)
07-28 05:25:06.447: ERROR/AndroidRuntime(348): at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 05:25:06.447: ERROR/AndroidRuntime(348): at android.os.Looper.loop(Looper.java:123)
07-28 05:25:06.447: ERROR/AndroidRuntime(348): at android.app.ActivityThread.main(ActivityThread.java:3647)
07-28 05:25:06.447: ERROR/AndroidRuntime(348): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 05:25:06.447: ERROR/AndroidRuntime(348): at java.lang.reflect.Method.invoke(Method.java:507)
07-28 05:25:06.447: ERROR/AndroidRuntime(348): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-28 05:25:06.447: ERROR/AndroidRuntime(348): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-28 05:25:06.447: ERROR/AndroidRuntime(348): at dalvik.system.NativeStart.main(Native Method)
07-28 05:25:06.447: ERROR/AndroidRuntime(348): Caused by: java.lang.NullPointerException
07-28 05:25:06.447: ERROR/AndroidRuntime(348): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
07-28 05:25:06.447: ERROR/AndroidRuntime(348): at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:353)
07-28 05:25:06.447: ERROR/AndroidRuntime(348): at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:348)
07-28 05:25:06.447: ERROR/AndroidRuntime(348): at com.org.EasyUpload.DownloadSnifferActivity.<init>(DownloadSnifferActivity.java:33)
07-28 05:25:06.447: ERROR/AndroidRuntime(348): at java.lang.Class.newInstanceImpl(Native Method)
07-28 05:25:06.447: ERROR/AndroidRuntime(348): at java.lang.Class.newInstance(Class.java:1409)
07-28 05:25:06.447: ERROR/AndroidRuntime(348): at android.app.ActivityThread.handleCreateService(ActivityThread.java:1901)
07-28 05:25:06.447: ERROR/AndroidRuntime(348): ... 10 more
What does the keyword
thismeans in this line?The context is not correct. You can’t write this line before the service is initialized. Just declare
SharedPreferences preferenceManager;in the class andpreferenceManager =inPreferenceManager.getDefaultSharedPreferences(this);
onCreate().