Android 開機自啟動樣本程式

來源:互聯網
上載者:User

Android 開機自啟動樣本程式

Android 開機自啟動樣本程式。使用廣播方式接受,採用Android內建儲存SharedPreferences儲存開機自啟動的設定。

本文源碼:點擊

1、先加上許可權

 

    

 

2、需要的廣播接收註冊(如果還要啟動服務,也先註冊)

 

 


 

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、簡單介面實現圖

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.