Android程式開機啟動&&監聽情景模式切換

來源:互聯網
上載者:User

 

要求:設定一個android應用程式開機啟動一個服務,此服務用來監聽情景模式的切換。

首先要知道在android中開機啟動程式是通過廣播機制實現的,在android手機啟動完成之後,系統會發送一個名叫android.intent.action.BOOT_COMPLETED的廣播,所以我們只要在程式中接收這個廣播,然後啟動一個後台服務,就會實現程式一開機即啟動。

對於監聽情景模式的切換,android手機跟其他手機不太一樣,沒有那些諸如多方通話、戶外模式、自訂模式等那麼多的模式。系統內建的只有正常模式跟靜音兩種可選,而且是長按關機按鈕才出現的。如:

一開始我以為系統也應該會發送一個廣播,但是找了很多資料沒找著。後來我從王濤那學到了一個不錯的技巧。假設你現在不知道系統到底會不會發情景模式切換的廣播,而且找了很多資料也沒有涉及諸如此類的問題,那麼現在你可以從LogCat中查看記錄檔。在我點擊中靜音切換的按鈕時,會得到如下日誌:

很明顯,系統是發了廣播的。所以我們同樣只要在程式中接收這個廣播就OK了。

Demo代碼如下:

MainActivity:

package org.sunchao;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);}}

BroadcastReceiver:

package org.sunchao;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class BootReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {// 監聽情景切換if (intent.getAction().equals("android.media.RINGER_MODE_CHANGED")) {Toast.makeText(context, "情景模式已切換", Toast.LENGTH_LONG).show();}// 設定開機啟動Intent intent2 = new Intent(context, BackgroundService.class);intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startService(intent2);}}

 

BackgroundService:

package org.sunchao;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;public class BackgroundService extends Service {private static String TAG = "BackgroundService";@Overridepublic void onCreate() {Log.i(TAG, "onCreate()");super.onCreate();}@Overridepublic void onStart(Intent intent, int startId) {Log.i(TAG, "onStart()");super.onStart(intent, startId);}@Overridepublic void onDestroy() {Log.i(TAG, "onDestroy()");super.onDestroy();}@Overridepublic IBinder onBind(Intent intent) {Log.i(TAG, "onBind()");return null;}}

 

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="org.sunchao" android:versionCode="1" android:versionName="1.0"><uses-sdk android:minSdkVersion="8" /><application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><service android:name=".BackgroundService" /><receiver android:name=".BootReceiver"><intent-filter><action android:name="android.intent.action.BOOT_COMPLETED" /><action android:name="android.media.RINGER_MODE_CHANGED" /></intent-filter></receiver></application><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /></manifest>

 

以上代碼開機啟動測試成功,查看後台啟動並執行服務(Boot_Complete):

進行模式切換監聽也是OK的:

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.