1.1、AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xinhua.bootservice" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name = "android.permission.GET_TASKS"/> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="com.xinhua.bootservice.MyBroadcast" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <service android:name="com.xinhua.bootservice.BootService" ></service> </application> </manifest>
1.2、MainActivity.java
package com.xinhua.bootservice; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // xhApiManager = new XHApiManager(); Intent intent = new Intent(MainActivity.this, BootService.class); startService(intent); Log.d("gatsby", "-------------------!!!!"); } }
1.3、MyBroadcast.java
package com.xinhua.bootservice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class MyBroadcast extends BroadcastReceiver { static final String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED"; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); //接受完开机自启的广播之后触发服务 if (action.equals(BOOT_COMPLETED)) { //启动Activity,实现开机自启动 // Intent intent1 = new Intent(context, MainActivity.class); //创建任务栈存放启动Activity // intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // context.startActivity(intent1); Intent startService = new Intent(context, BootService.class); Log.d("eric", "CrushBroadcast-----------!!!!"); context.startService(startService); } } }
1.4、BootService.java
package com.xinhua.bootservice; import java.io.DataInputStream; import java.io.DataOutputStream; import java.util.Calendar; import java.util.TimerTask; import com.android.xhapimanager.XHApiManager; import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class BootService extends Service { private String TAG = "gatsby"; private String outPackageInfo; private String PACKAGE_NAME = "com.gongfubao.attendance"; private String PACKAGE_ACTIVITY = "com.gongfubao.attendance.ui.StartActivity"; // XHApiManager apimanager; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); Log.d(TAG, "onCreate"); // ApplicationLauncher(PACKAGE_NAME,PACKAGE_ACTIVITY); SetRtc_Time(); // apimanager = new XHApiManager(); } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); /* * MyTimerTask timerTask = new MyTimerTask(); Timer timer = new Timer(true); * timer.schedule(timerTask, 0, 60000);// 定时每30秒执行一次 */ } public void SetRtc_Time() { Runnable run = new Runnable() { @Override public void run() { while (true) { Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int miao = calendar.get(Calendar.SECOND); // Log.d(TAG,"hour"+hour); // Log.d(TAG,"minute"+minute); // Log.d(TAG,"miao"+miao); if (2 == hour && 59 == minute) { // Log.d(TAG,"18==hour && 30==minute"); sendBroadcast(new Intent().setAction("")); // apimanager.XHReboot(); // break; } try { Thread.sleep(1000);// 设置间隔 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }; Thread t1 = new Thread(run); t1.start(); } public class MyTimerTask extends TimerTask { @Override public void run() { String packageInfo = RootCommand("dumpsys window | grep mCurrentFocus"); Log.d(TAG, "packageInfo = " + packageInfo); if (!(packageInfo.contains(PACKAGE_NAME))) { Log.d(TAG, "PACKAGE_NAME============11111 "); ApplicationLauncher(PACKAGE_NAME, PACKAGE_ACTIVITY); } } } private boolean ApplicationLauncher(String PackageName, String ActivityName) { ComponentName componentName = new ComponentName(PackageName, ActivityName); Intent intent = new Intent(Intent.ACTION_MAIN); intent.setComponent(componentName); if (getApplication().getPackageManager().resolveActivity(intent, 0) == null) { return false; // no this activity } intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); getApplication().startActivity(intent); return true; } private String RootCommand(String cmd) { Process process = null; DataOutputStream os = null; DataInputStream is = null; try { process = Runtime.getRuntime().exec("su"); os = new DataOutputStream(process.getOutputStream()); os.writeBytes(cmd + " "); os.writeBytes("exit "); os.flush(); int aa = process.waitFor(); is = new DataInputStream(process.getInputStream()); byte[] buffer = new byte[is.available()]; is.read(buffer); outPackageInfo = new String(buffer); Log.d(TAG, outPackageInfo); } catch (Exception e) { e.printStackTrace(); } finally { try { if (os != null) { os.close(); } if (is != null) { is.close(); } process.destroy(); } catch (Exception e) { } } return outPackageInfo; } }