![](/img/trans.png)
[英]I want to start a service only when a new picture is clicked in android. How do I go about doing that?
[英]I am new to android. I wish to use android service for blocking call only when the service is started
这是仅在服务运行时才应运行的代码。 并且该服务应在其他部分停止。
public void onReceive(Context context, Intent intent)
{
this.context=context;
this.intent=intent;
if (!intent.getAction().equals("android.intent.action.PHONE_STATE"))
return;
else
{
number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (Callblockactivity.blockList.contains(new Blacklist(number))) {
System.out.println("i");
}
else {
disconnectPhoneItelephony(context);
return;
}
}
}
现在,应按如下所示在mainactivity中启动该服务。
if (speed >10) {
//service should be started.
}
else {
//service should be stopped
}
请帮忙。
使用startService启动服务。 使用stopSelf或stopService停止服务。
而且,您可以在服务内部使用静态布尔变量,以保留该服务是否正在运行的标志。 除了此变量,您还可以在启动服务时动态注册一个接收器,并在停止服务时注销它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.