[英]I am new to android. I wish to use android service for blocking call only when the service is started
This is the code should be run only when the service is running. 这是仅在服务运行时才应运行的代码。 And the service should be stopped in some other part.
并且该服务应在其他部分停止。
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;
}
}
}
Now, the service should be started in mainactivity as follow. 现在,应按如下所示在mainactivity中启动该服务。
if (speed >10) {
//service should be started.
}
else {
//service should be stopped
}
Please help with this. 请帮忙。
use startService to start a Service. 使用startService启动服务。 Use stopSelf or stopService to stop your service.
使用stopSelf或stopService停止服务。
And you may use a static boolean variable inside your service in order to keep the flag whether the service is running. 而且,您可以在服务内部使用静态布尔变量,以保留该服务是否正在运行的标志。 Instead of that variable also you may dynamically register a receiver when you start your service, and unregister it when you stop the service.
除了此变量,您还可以在启动服务时动态注册一个接收器,并在停止服务时注销它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.