一、实现的方法是开机接收开机完成广播然后打开app,packages\apps\SettingsIntelligence\src\com\android\settings\intelligence\search\SearchFragment.java
Intent intent = new Intent();
intent.setComponent(ComponentName.unflattenFromString("com.autoreboot.android/com.autoreboot.android.MainActivity"));
try {
startActivity(intent);
} catch (Exception ex) {
}
二、reboot app源码
1、AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
android:versionCode="2" android:versionName="1.2" package="com.autoreboot.android"
xmlns:android="http://schemas.android.com/apk/res/android"
coreApp="true"
android:sharedUserId="android.uid.system">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="MainActivity" android:windowSoftInputMode="adjustUnspecified|stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<receiver android:name="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.REBOOT" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
</manifest>
2、src\com\autoreboot\android\BootReceiver.java
package com.autoreboot.android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
public class BootReceiver extends BroadcastReceiver{
private int isAutoStart;
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences pm = PreferenceManager.getDefaultSharedPreferences(context);
if(pm == null)
{
isAutoStart=0;
}
else
{
isAutoStart = pm.getInt("appautostart", 0);
}
if(isAutoStart == 1)
{
Intent localIntent = new Intent(context, MainActivity.class);
localIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(localIntent);
}
}
}
3、Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
# Only compile source java files in this apk.
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_PACKAGE_NAME := AutoReboot
LOCAL_CERTIFICATE := platform
LOCAL_SDK_VERSION := current
include $(BUILD_PACKAGE)
# Use the following include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))
4、src\com\autoreboot\android\MainActivity.java
package com.autoreboot.android;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.PowerManager;
import android.content.Context;
public class MainActivity extends Activity {
private static final String LOG_TAG = "RebootMainActivity";
private static final String TOTAL_TIMES = "TotalTimes";
private static final String REBOOT_TIMES = "RebootTimes";
private static final String REBOOT_FREQUENCY = "RebootFrequencey";
private static final String APP_AUTOSTART = "appautostart";
private int totalTimes;
private int rebootTimes;
private int rebootFrequency;
private int count;
private boolean isAutoRebootCancel;
private int isAutoStart;
private EditText timesEditText;
private EditText frequencyEditText;
private Button clearBtn;
private Button cancelBtn;
private TextView timesAndFrequencyText;
private static final int UpdateMsgText = 1;
private SharedPreferences prefs;
private Handler timerHandler;
private Runnable myTimerThread;
private PowerManager pm;
private PowerManager.WakeLock wl;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case UpdateMsgText:
String secondStr = msg.obj.toString();
showMsgText(Integer.valueOf(secondStr));
break;
default:
break;
}
super.handleMessage(msg);
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
if(pm!=null){
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AutoReboot");
if(wl!=null){
wl.acquire();
}
}
setupViews();
if (prefs == null) {
prefs = PreferenceManager.getDefaultSharedPreferences(this);
}
read_status();
if(rebootTimes > totalTimes){
readDefaultValues();
init();
isAutoStart = 0;
save_isAutoStart();
showMsgText(rebootFrequency);
cancelBtn.setText(getString(R.string.set_auto));
isAutoRebootCancel = true;
timesEditText.setEnabled(true);
frequencyEditText.setEnabled(true);
}else{
init();
isAutoStart = 1;
save_isAutoStart();
showMsgText(rebootFrequency);
timerHandler.postDelayed(myTimerThread, 1000);
}
}
@Override
protected void onDestroy() {
if(wl!=null&&wl.isHeld()){
wl.release();
}
super.onDestroy();
}
private void setupViews() {
timesEditText = (EditText) findViewById(R.id.timesText);
frequencyEditText = (EditText) findViewById(R.id.frequencyText);
clearBtn = (Button) findViewById(R.id.clear);
cancelBtn = (Button) findViewById(R.id.cancle);
timesAndFrequencyText = (TextView) findViewById(R.id.msgText);
clearBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
timerHandler.removeCallbacks(myTimerThread);
readDefaultValues();
}
});
cancelBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
isAutoRebootCancel = !isAutoRebootCancel;
if (isAutoRebootCancel) {
isAutoStart = 0;
save_isAutoStart();
timerHandler.removeCallbacks(myTimerThread);
cancelBtn.setText(getString(R.string.set_auto));
timesEditText.setEnabled(true);
frequencyEditText.setEnabled(true);
} else {
isAutoStart = 1;
save_isAutoStart();
String totalTimesStr = timesEditText.getText().toString()
.trim();
totalTimes = Integer.valueOf(totalTimesStr);
String frequencyStr = frequencyEditText.getText()
.toString().trim();
rebootFrequency = Integer.valueOf(frequencyStr);
count = rebootFrequency;
timerHandler.postDelayed(myTimerThread, 1000);
cancelBtn.setText(getString(R.string.move_auto));
timesEditText.setEnabled(false);
frequencyEditText.setEnabled(false);
}
}
});
}
private void init() {
count = rebootFrequency;
timesAndFrequencyText.setText(R.string.move_auto);
timesEditText.setText(String.valueOf(totalTimes));
timesEditText.setEnabled(false);
frequencyEditText.setText(String.valueOf(rebootFrequency));
frequencyEditText.setEnabled(false);
isAutoRebootCancel = false;
cancelBtn.setText(getString(R.string.move_auto));
if (timerHandler == null) {
timerHandler = new Handler();
}
if (myTimerThread == null) {
myTimerThread = new Runnable() {
@Override
public void run() {
if (count > 0) {
try {
sendMsg(UpdateMsgText, String.valueOf(count--));
} catch (Exception e) {
e.printStackTrace();
}
timerHandler.postDelayed(myTimerThread, 1000);
} else {
startReboot();
}
}
};
}
}
private void showMsgText(int leftSeconds) {
/*
String msg1 = getString(R.string.msg1);
String msg3 = getString(R.string.msg3);
String msg4 = getString(R.string.msg4);
String msg2 = getString(R.string.msg2);
String result = msg1 + String.valueOf(rebootTimes) + msg3
+ String.valueOf(totalTimes - rebootTimes) + msg4
+ String.valueOf(leftSeconds) + msg2;
*/
timesAndFrequencyText.setText(getString(R.string.msg1, rebootTimes,totalTimes - rebootTimes,leftSeconds));
}
private void startReboot() {
rebootTimes++;
save_status(rebootTimes, rebootFrequency, totalTimes , isAutoStart);
Log.d(LOG_TAG, "reboot...");
PowerManager pm = (PowerManager)getSystemService(
Context.POWER_SERVICE);
pm.reboot(null);
}
@Override
protected void onPause() {
Log.d(LOG_TAG, "pause...");
super.onPause();
}
private void save_isAutoStart()
{
Editor editor = prefs.edit();
editor.putInt(APP_AUTOSTART, isAutoStart);
editor.commit();
}
/** Read data from APK. */
private void read_status() {
this.rebootTimes = prefs.getInt(REBOOT_TIMES, 0);
this.rebootFrequency = prefs.getInt(REBOOT_FREQUENCY, 10);
this.totalTimes = prefs.getInt(TOTAL_TIMES, 5000);
}
private void readDefaultValues(){
this.rebootTimes = 1;
this.rebootFrequency = 20;
this.totalTimes = 20;
this.count = this.rebootFrequency;
this.isAutoStart = 1;
}
/** Save data to APK. */
public void save_status(int rebootTimes, int rebootFrequency, int totalTimes ,int isAutoStart) {
Editor editor = prefs.edit();
editor.putInt(REBOOT_TIMES, rebootTimes);
editor.putInt(REBOOT_FREQUENCY, rebootFrequency);
editor.putInt(TOTAL_TIMES, totalTimes);
editor.putInt(APP_AUTOSTART, isAutoStart);
editor.commit();
}
/** Send message to UI thread. */
private void sendMsg(int type, String str) {
Message msg = mHandler.obtainMessage();
msg.what = type;
msg.obj = str;
mHandler.sendMessage(msg);
}
}
5、xml布局文件
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_centerHorizontal="true">
<TableRow android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:textSize="15.0dip" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/preTimes" />
<EditText android:textSize="15.0dip" android:id="@+id/timesText"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:numeric="integer" />
</TableRow>
<TableRow android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:textSize="15.0dip" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/prefrequency" />
<EditText android:textSize="15.0dip" android:id="@+id/frequencyText"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:layout_below="@+id/timesText" android:numeric="integer" />
</TableRow>
</TableLayout>
<TextView android:textSize="25.0dip" android:id="@+id/msgText"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<Button android:id="@+id/clear" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginLeft="20.0dip"
android:layout_marginBottom="20.0dip" android:text="@string/clear"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true" />
<Button android:id="@+id/cancle" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginRight="20.0dip"
android:layout_marginBottom="20.0dip" android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>