我已經編寫了一個android服務,我需要在後臺運行並且能夠與應用程序的其餘部分進行通信。代碼如下所示。問題是,當服務被調用時,應用程序關閉。而且很顯然,它不是到達服務本身沒有我已經寫在的onCreate,在onStart等日誌命令被印刷Android應用程序強制在服務被調用時關閉
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();
}
}
}
我也取得了在AndroidManifest.xml中輸入以下內容:
<service android:enabled="true" android:name=".DownloadSnifferActivity">
</service>
而且這是我撥打電話到服務來啓動它:
startService(new Intent(EasyUploadMainMenu.this, DownloadSnifferActivity.class));
異常登錄的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
在logcat中顯示什麼?應該有一個例外,指出問題。 –
它以某種方式不記錄任何東西。在我清理了我的LogCat控制檯一次後,它沒有出現。 – SThomas