Android(安卓)开机自启动示例程序
16lz
2022-04-06
Android 开机自启动示例程序。使用广播方式接受,采用Android自带存储SharedPreferences存储开机自启动的设置。
本文源码:点击
1、先加上权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2、需要的广播接收注册(如果还要启动服务,也先注册)
<!-- 开机自启动广播接受 --> <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>
3、广播接收AutoStartBroadcastReceiver
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;@Overridepublic 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);}}}}
4、如果需要启动一些服务再写(可选项)
如果程序需要启动一些必要的服务再写这个也可以,一般开机自启动只需要启动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 {@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;} @Override public void onCreate(){ super.onCreate(); Log.d("TAG2","test service"); }}
5、怎样使用这些配置MainActivity
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;@Overrideprotected 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() {@Overridepublic void onClick(View v) { Editor editor = mPreferences.edit(); editor.putBoolean("AddToAuto", true); editor.commit(); textView1.setText("已打开开机自启动");}});//关闭findViewById(R.id.button2).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) { Editor editor = mPreferences.edit(); editor.putBoolean("AddToAuto", false); editor.commit(); textView1.setText("已关闭开机自启动");}});}@Overridepublic 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;}}
6、简单界面实现图
到此结束,本文源码:点击
更多相关文章
- android 开机动画修改以及默认壁纸
- Android(安卓)无线启动过程分析 无线启动过程分析
- android实现开机自启动服务
- android 2.3 4.0 添加开机音乐
- Android启动后遇到错误
- Android根据电量变化为不同图片的方法【电池电量提示】
- Android(安卓)启动浏览器的方法
- Android启动界面实现
- Activity的启动模式(android:launchMode)