To write a service, which has a thread, start a program for monitoring, if the array is met, then return to the main interface. Achieve the purpose of disabling the program. Do not answer the phone normally, then pick up the phone is invalid. . . Solving.
public class TimerService extends Service {
private ActivityManager am;
Handler handler = new Handler ();
String [] selectedPackageName;
private Thread clockThread;
private int timer = 0;
private int [] a;
private boolean isRunning = true;
@ Override
public IBinder onBind (Intent intent) {
/ / TODO Auto-generated method stub
return null;
}
@ Override
public void onCreate () {
super.onCreate ();
}
public void onStart (Intent intent, int startId) {
am = (ActivityManager) getApplication () getSystemService (Context.ACTIVITY_SERVICE);.
selectedPackageName = intent.getStringArrayExtra ("listSelectedPackageName");
a = intent.getIntArrayExtra ("theTime"); / / get pass from the activity fed to the array
Log.e ("TheTime", "time" + a [0] * 60 + "seconds");
clockThread = new Thread (new Runnable () {
@ Override
public void run () {
while (isRunning) {
try {
Thread.sleep (1000);
timer + +;
Log.d ("tag", "passed" + timer + "seconds");
for (int i = 0; i
if (tasksInfo.get (0) topActivity.getPackageName () equals (selectedPackageName [i]) |.. |. tasksInfo.get (0) topActivity.getPackageName () equals ("com.qin.appsize").) {< br />/ / Return to the main interface
Intent in = new Intent (Intent.ACTION_MAIN);
in.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
in.addCategory (Intent.CATEGORY_HOME);
startActivity (in);
}
}
if (timer == a [0] * 60) {
Log.e ("TheTime", "set time" + timer + "seconds to");
isRunning = false;
}
} Catch (InterruptedException e) {
e.printStackTrace ();
}
}
}
});
clockThread.start (); / * start thread * /
}
@ Override
public void onDestroy () {
/ / TODO Auto-generated method stub
super.onDestroy ();
}
}<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Pick up the phone service is still running it? There continues to output those log?
Reply:
Is invalid or suspended service to be killed?
Reply:
Do not continue to output log. . . Service stopped it. . .
Reply:
Do not continue to output log. . .
Reply:
?log did not continue to export, is not very clear in the end is to kill or be suspended
Reply:
The notification can be used to increase the priority of service, reducing the probability of the system is recovered.
In the Service of the onCreate () or onStartCommand () method, the call startForeground (int id, Notification notification). id NOTIFY_ID not equivalent to 0, the role can be used to find this notification, it is easy to operate; notification prompts the user of your service at the front desk.
Notification is still long, you will be able to maintain a high service priority, the system can not be easily recovered.
stopForeground () can set the service back to normal priority.
Reply:
into a reception after the service really can, thank you, just a small road rookie
No comments:
Post a Comment