Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot receive broadcast in ICS

I writing 1 app for Android 4.0, and it's started via broadcastreceiver. My code is below:

In AndroidManifest.xml:

 <uses-permission android:name="android.permission.INTERNET" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <receiver android:name="com.Android.Exercise.StartUpReceiver"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <!--<action android:name="StartInstrument" /> 
                <action android:name="PrintControlName" />      -->     
            </intent-filter>
        </receiver>         
        <service android:enabled="true" android:name="StartAUT_Service">
            <intent-filter>
                <action android:name="com.Android.Exercise.StartAUT_Service" />
            </intent-filter>
        </service>  
    </application>

In StartUpReceiver class:

public class StartUpReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {

        Log.i("Broadcast", "onReceive");

        Intent i = null;

        if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
             i = new Intent(context, StartAUT_Service.class);          
             i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }

        context.startService(i);            
    }       
}

After I rebooted my device, I can not receive broardcast. Please help me, thank so much

like image 508
user1266236 Avatar asked Mar 14 '12 03:03

user1266236


2 Answers

Starting from Android 3.0, application state was introduced. All applications are now installed with a state set to inactive, making all it's BroadcastReceivers inactive.

To make the state as active, the user must launch the application at least once and the OS will automatically activate the app and all its BroadcastReceivers.

In your case, you need to add an activity, be it a license agreement or a help page. This will create an icon for your app for the user to click and activate the app.

Note that the application's state will be set to back inactive if the user force-closes the application.

I faced a similar problem: Android ICS not receiving data sms

I hope that solves your problem or at least put you on the right track.

regards, Ahmad

like image 151
Shatazone Avatar answered Sep 23 '22 15:09

Shatazone


Try

 <uses-permission android:name="android.intent.action.BOOT_COMPLETED" />

below Internet's uses permission

like image 22
Sadeshkumar Periyasamy Avatar answered Sep 23 '22 15:09

Sadeshkumar Periyasamy