Android 开机自启动示例程序。使用广播方式接受,采用Android自带存储SharedPreferences存储开机自启动的设置。
本文源码:点击
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- 开机自启动广播接受 --> <receiver android:name="com.example.autostart.AutoStartBroadcastReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <!-- 开机自启动服务--> <service android:name="com.example.autostart.AutoStartService" android:label="AutoStartService" android:enabled="true" android:exported="true" android:process=":remote"> </service>
package com.example.autostart; import android.content.BroadcastReceiver; import android.content.Context; import android.content.ContextWrapper; import android.content.Intent; import android.content.SharedPreferences; //开机自启动广播接受 public class AutoStartBroadcastReceiver extends BroadcastReceiver { private static final String ACTION = "android.intent.action.BOOT_COMPLETED"; private SharedPreferences mPreferences = null; @Override public void onReceive(Context context, Intent intent) { mPreferences = context.getSharedPreferences("AutoStart", ContextWrapper.MODE_PRIVATE); if (intent.getAction().equals(ACTION)) { if (mPreferences.getBoolean("AddToAuto", false)) { //后边的XXX.class就是要启动的服务 Intent service = new Intent(context,AutoStartService.class); context.startService(service); // 启动应用,参数为需要自动启动的应用的包名,只是启动app的activity的包名 Intent newIntent = context.getPackageManager() .getLaunchIntentForPackage("com.example.autostart"); context.startActivity(newIntent); } } } }
如果程序需要启动一些必要的服务再写这个也可以,一般开机自启动只需要启动app的主activity。这里示范一下写服务。
package com.example.autostart; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.ContextWrapper; import android.content.Intent; import android.content.SharedPreferences; import android.os.IBinder; import android.util.Log; //开机自启动广播接受 public class AutoStartService extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate(){ super.onCreate(); Log.d("TAG2","test service"); } }
package com.example.autostart; import android.os.Bundle; import android.app.Activity; import android.content.ContextWrapper; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; public class MainActivity extends Activity { private SharedPreferences mPreferences = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPreferences = getSharedPreferences("AutoStart",ContextWrapper.MODE_PRIVATE); boolean bStart = mPreferences.getBoolean("AddToAuto", false); final TextView textView1 = (TextView)findViewById(R.id.textView1); if (bStart) { textView1.setText("已打开开机自启动"); }else { textView1.setText("已关闭开机自启动"); } //打开 findViewById(R.id.button1).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Editor editor = mPreferences.edit(); editor.putBoolean("AddToAuto", true); editor.commit(); textView1.setText("已打开开机自启动"); } }); //关闭 findViewById(R.id.button2).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Editor editor = mPreferences.edit(); editor.putBoolean("AddToAuto", false); editor.commit(); textView1.setText("已关闭开机自启动"); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
到此结束,本文源码:点击
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/qq_16064871/article/details/49555991