Sun*_*hoo 2 service android process
我创建了一项服务和一项活动.在那个活动中,我有两个按钮.一个是开始服务而另一个是停止服务.我可以通过调用startService()
但无法使用停止服务来启动远程活动stopService()
.如果我点击开始按钮,我发现额外的远程进程运行(使用eclipse ide).我期待如果我点击停止按钮,那么额外的过程会停止.但它没有发生.我能够成功调用启动和停止服务方法.为了验证代码,我在每个启动和停止方法中添加了一个Toast消息.如何停止远程服务?
public class SimpleServiceController extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button start = (Button)findViewById(R.id.serviceButton);
Button stop = (Button)findViewById(R.id.cancelButton);
start.setOnClickListener(startListener);
stop.setOnClickListener(stopListener);
}
private OnClickListener startListener = new OnClickListener() {
public void onClick(View v){
startService(new Intent(SimpleServiceController.this,SimpleService.class));
}
};
private OnClickListener stopListener = new OnClickListener() {
public void onClick(View v){
stopService(new Intent(SimpleServiceController.this,SimpleService.class));
}
};
}
Run Code Online (Sandbox Code Playgroud)
public class SimpleService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
}
}
Run Code Online (Sandbox Code Playgroud)
<service android:name=".SimpleService" android:process=":remote">
</service>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4015 次 |
最近记录: |