Wednesday, March 5, 2014

android service problems


Here is one of my start automatically after stop service
 startBrew (); 
/ / Create Intent
intent = new Intent ();
/ / Set the Action property
intent.setAction ("com.qin.appsize.MY_SERVICE");

/ / Execute service
/ / Execute the timer
mTimer = new Timer ();
mTimerTask = new TimerTask () {
String timeLimit = (String) brewTimeLabel.getText () toString ();.
int i = Integer.parseInt (timeLimit.substring (0, timeLimit.length () -1)) * 60;
public void run () {
/ / Print time
Log.d ("AndroidTimerDemo", "timer:" + formatDate ());
/ / Set its own stop function
/ / Start the Service
startService (intent);
if (i <1) {
stopService (intent);
System.out.println ("Service Thread automatically stop");
mTimer.cancel ();
}
System.out.println ("Service Thread countdown:" + i);
i -;
}
};
mTimer.schedule (mTimerTask, 0, 1 * 1000);
BrewClockActivity.this.finish ();
}
}


Here is my service
 package com.qin.appsize; 

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;


public class TimerService extends Service {
@ 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) {
Log.v ("=========", "***** DaemonService *****: onStart");
/ / Here you can do the right thing
Service
System.out.println ("I'm doing!");
}
@ Override
public void onDestroy () {
/ / TODO Auto-generated method stub
super.onDestroy ();
}

}

After the run is now executed once every second onStart () method to print "I'm doing!"
I think "I'm doing!" Where did the end of the specified process functions should be how to do that is at the end of every second that I specify (such as specifying an alarm clock program) process.
Great God seeking advice on how to do<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
You want to do some action in the service right inside.
You do not write code directly on the line, whatever you think need to write your own logic, such as start another program, or close another program.
]]
If understood correctly?
Reply:
The problem now is that I tried not to run after several close method to write code service
So I mainly want to ask one: onStart method in that the end of the specified process can write what
method2: Are there any examples of end of the process. Thank
Reply:
reference to the second floor u010384801 reply:
is the question now is not run after I tried several ways to close the write code service
So I mainly want to ask one: onStart method in that the end of the specified process can write what
method2: Are there any examples of end of the process. Thank

In the immediate post-tone finish inside onstart stopSelf () or stopService ()
Reply:
How to write the end of the process? There are no examples thank
In the top end of the timer, then there
himself to the end of timeBefore the interview, I use several methods such as this actManager.restartPackage ("com.commonsware.Now");
Specify the end com.commonsware.Now package process executed once every second, but not being given the run
I am a novice just learning android Great God enlighten ah
Reply:
No one yet top
Reply:
The landlord would like nothing more than to kill the specified process that needs it?
1, kill the process themselves
System.exit (0);
android.os.Process.killProcess (android.os.Process.myPid ());
2, according to the package name, kill another process
ActivityManager am = (ActivityManager) mContext.getSystemService (Context.ACTIVITY_SERVICE);
am.forceStopPackage ("specify the package name");
Need to add permissions androidmanifest.xml in:

The landlord should require the second.
Reply:
reference to the 6th floor ncepu307 reply:
landlord would like nothing more than to kill the specified process that needs it?
1, kill the process themselves
System.exit (0);
android.os.Process.killProcess (android.os.Process.myPid ());
2, according to the package name, kill another process
ActivityManager am = (ActivityManager) mContext.getSystemService (Context.ACTIVITY_SERVICE);
am.forceStopPackage ("specify the package name");
Need to add permissions androidmanifest.xml in:

The landlord should require the second.

Why is there with a second method that can not be adjusted
Reply:
Haha
Thank you to get a floor under the Supplemental forceStopPackage 6 The second method is a hidden method can not be directly invoked
Therefore, the need to use reflection
 actManager = (ActivityManager) this.getSystemService (Context.ACTIVITY_SERVICE); 

Method method;
try {
method = Class.forName ("android.app.ActivityManager") getMethod ("forceStopPackage", String.class);.
method.invoke (actManager.getClass (), "com.android.camera");
} Catch (NoSuchMethodException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
} Catch (ClassNotFoundException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
} Catch (IllegalArgumentException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
} Catch (IllegalAccessException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
} Catch (InvocationTargetException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}

No comments:

Post a Comment