简体   繁体   中英

Broadcast Receiver Not working in 4.1.1

I have a broadcast receiver for incoming call.I want to launch a new activity when an incoming call comes.I am aware of the changes that are made from android 3.0,that the broadcast receiver will not work unless user manually starts an application For that purpose I launch a dummy activity with just a toast message in it.Still the broadcast receiver is not working. Here is my code

My broadcastreceiver

public class IncomingCallResult extends BroadcastReceiver 
{
String TAG="IncomingCallResult";    
@Override
public void onReceive(Context arg0, Intent I1) 
{
    Log.i(TAG,"inside on receive........");
    Bundle bundle=I1.getExtras();
    String state=bundle.getString(TelephonyManager.EXTRA_STATE);

    if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
    {
        Intent flash_intent=new Intent(arg0,LedFlasher.class);
        flash_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        arg0.startActivity(flash_intent);

    } 


}    

}

manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.blinker"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver 
        android:name=".IncomingCallResult"
        android:enabled="true">
        <intent-filter
            android:priority="214783648" 
            android:name="android.intent.action.PHONE_STATE">
        </intent-filter>
    </receiver>

    <activity
        android:name=".LedFlasher"
        android:label="@string/title_activity_incoming_call_result" >     
     </activity>

    <activity
        android:name=".Dummy">
         <intent-filter >
             <action android:name="android.intent.action.MAIN"/>
             <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>     
     </activity>



</application>

What is wrong with the code? Please help

As you have already known that the broadcast receiver will not work unless user manually starts an application For that purpose , you should not be surprised that your broadcast receiver will not work until user Manually open your application once . That is to say, you have to make a launcher activity which user is able to click and open it manually .

And what's more, it is better to Open and stay in your application for like 20s since I remember that the change of application configuration will take 10 or 20s to be saved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM