Start the Android program and switch the listening mode

Source: Internet
Author: User

 

Requirement: Set an android Application to start a service when it is started. This service is used to listen for switching in the context mode.

First, you must know that the launch program in android is implemented through the broadcast mechanism. After the android mobile phone is started, the system will sendAndroid. intent. action. BOOT_COMPLETEDSo we only need to receive the broadcast in the program, and then start a background service, the program will be started as soon as it is started.

For switching the listening scene mode, android phones are not the same as other mobile phones, and there are not so many modes, such as meeting mode, outdoor mode, and custom mode. The system has two built-in options: Normal Mode and mute mode, and long-pressed shutdown button appears. For example:

At first I thought the system should also send a broadcast, but I did not find a lot of information. Later, I learned a good skill from Wang Tao. If you do not know whether the system will broadcast a Scenario-Based Switching broadcast, and you have found a lot of information that does not involve such issues, you can now view the log files from LogCat. When I click the switch button in mute mode, the following logs are displayed:

Obviously, the system broadcast. So we only need to receive the broadcast in the program.

The Demo code is as follows:

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) {// listener scenario switch if (intent. getAction (). equals ("android. media. RINGER_MODE_CHANGED ") {Toast. makeText (context, "scene mode switched", Toast. LENGTH_LONG ). show () ;}// set 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>

 

Run the above Code to start the test successfully. view the services running in the background (Boot_Complete ):

The mode switching listener is also OK:

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.