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.
if (speed >10) {
//service should be started.
}
else {
//service should be stopped
}
Please help with this.
use startService to start a Service. Use stopSelf or stopService to stop your service.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.