这是Service
来自Activity
按钮点击的Activity
。如果我在Service
正在运行时向左滑动android:process=":remote"
,则会崩溃。我还尝试在清单中放入@Override
public void onCreate(){
super.onCreate();
Log.d("Service", "Creating");
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return null;
}
@Override
public int onStartCommand (Intent intent, int flags, int startId)
{
super.onStartCommand(intent, flags, startId);
Log.d("Started", "Service");
type = intent.getIntExtra("Type",-1);
mode = intent.getIntExtra("Mode",-1);
rank = intent.getIntExtra("Rank", -1);
latitude = intent.getDoubleExtra("Lat", -1.0);
longitude = intent.getDoubleExtra("Long", -1.0);
startTime = intent.getLongExtra("Start", 0);
endTime = intent.getLongExtra("End", 0);
,但在单独的过程中运行它,但它仍然是相同的。
java.lang.RuntimeException: Unable to start service com.routofy.routofytest.MyLocationService@374afe93 with null: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2913)
at android.app.ActivityThread.access$2100(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
at com.routofy.routofytest.MyLocationService.onStartCommand(MyLocationService.java:89)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2896)
at android.app.ActivityThread.access$2100(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
我得到的错误是:
null
这是type = intent.getIntExtra("Type",-1);
Intent
指针
我了解到,杀死该应用程序会导致该过程失败,而Service
此null
正在逐渐成为Activity
。但是如何处理即使Intent
被杀Service
Activity
被移交给Intent pickIntent = new Intent(getApplicationContext(),MyLocationService.class);
的情况呢?
我希望服务完全独立于{{1}}。我也在运行它:
{{1}}
答案 0 :(得分:3)
据我所知,杀死该应用程序正在杀死该服务正在获取的进程和意图将为空。
由于您尚未发布onStartCommand()
的返回值,我认为它是START_STICKY
,这意味着系统会在传入null
Intent
的情况下重新创建服务。
但是如何处理这样的情况,即使活动被杀死意图也会被移交给服务。
只需返回START_REDELIVER_INTENT
标志,即:
“如果系统在onStartCommand()
返回后终止服务,请重新创建服务并使用提供给服务的最后一个意图调用onStartCommand()
。”