Rob*_* IV 10 service android background-process
我有一个应用程序,它有一个Activity,用作常规GUI和一个服务.我的活动有两个按钮.一个按钮停止进程,一个按钮终止进程.我分别使用这些方法来启动和停止我的进程:
Intent i = null;
Button start;
Button stop;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
i = new Intent(this, Service.class);
start = (Button) findViewbyId(R.id.start_button);
stop = (Button) findViewById(R.id.stop_button);
start.setOnClickListener(new OnClickListener(){
public void onClick(){
startService(i);
}
}
stop.setOnClickListener(new OnClickListener(){
public void onClick(){
stopService(i);
}
}
}
Run Code Online (Sandbox Code Playgroud)
此服务不受actvivty或app的约束.我在Manifest中配置了这样的服务:
<service
android:name="com.example.mypackage.Service"
android:process=":remote">
<intent-filter>
<action
android:name="com.example.mypackage.Service" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
当我启动服务时,它独立运行在其他任何东西上.这意味着,当我启动服务和onDestroy()应用程序时,服务仍在运行.我可以看到它仍在运行,因为通过adb,我运行ps命令,它说它是.
问题是我打电话的时候stopService(intent).当我stopService(intent)从活动方面调用时,它将运行onDestroy()服务中设置的代码行,但是当我运行ps时adb,它表示它仍在运行.
我希望能够完全破坏服务.我不确定服务是如何运作的,所以请不要犹豫,采取补救措施.我不确定它是否与我在服务中所做的事情有关.谢谢!
编辑:
当我启动服务时,我使用onStartCommand()并在那里运行一些代码.我也回来START_STICKY了onStartCommand().我也试过返回START_NOT_STICKY,我打电话后服务仍在运行startService(intent).
小智 6
如果您想要强制终止服务进程,可以使用以下代码:
Process.killProcess(Process.myPid());
Run Code Online (Sandbox Code Playgroud)
小智 -1
我认为您可以android:exported="false"在清单中进行设置,以便其他应用程序无法访问该服务从而启动该服务。我可能需要查看代码的完整实现才能完全确定其原因。我做了同样的事情,而且效果非常好。
| 归档时间: |
|
| 查看次数: |
21010 次 |
| 最近记录: |