Manifest.xml文件:
复制代码<service
android:name=".DaemonService"
android:enabled="true"
android:process=".DaemonService" >
<intent-filter android:priority="1000">
<action android:name="cn.test.DaemonService" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
注释:因为我们这个服务要一直在后台运行,所以不采用bindService的方式,而是直接采用startService的方式。
这样就不至于我们的程序结束,也把我们的服务也结束掉了。
复制代码
package cn.start.test;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.Service;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
public class DaemonService extends Service {
private static final String TAG = "Alarmreceiver";
Handler hd1 = new Handler();
int delay = 5000;
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@SuppressLint("NewApi")
public void onCreate() {










