Zakharov Roman
Zakharov Roman

Reputation: 779

How can I start an Intent via Broadcast Receiver`s method onReceive

I have a problem with broadcast receiver. it doesn`t start an activity from onReceive() method this is my Manifest:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <receiver 
        android:name=".MyBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
        </intent-filter>
    </receiver>
    <activity
        android:name=".BatteryActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity 
        android:name=".ChargeConnected">
        <intent-filter>
            <action android:name="gang.my_package.Battery.CHARGE_CONNECTED"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>
</application>

and this is my method onReceive():

public void onReceive(Context context, Intent intent) 
{
    // TODO Auto-generated method stub
    Intent myIntent = new Intent("gang.my_package.Battery.CHARGE_CONNECTED");
    context.startActivity(myIntent);
    //Toast.makeText(context, "Hello World!", Toast.LENGTH_LONG).show();
}

when I try to make a Toast, it works. but if I try to start a new activity, it does not work. so what`s the problem? help please

Upvotes: 0

Views: 404

Answers (1)

mah
mah

Reputation: 39797

The problem is that your myIntent variable doesn't describe an activity to start. http://www.androidcompetencycenter.com/2009/03/tutorial-how-to-start-a-new-activity/ has one example of describing a valid Activity intent (for an Activity that is within your current application). How to start activity in another application? shows creating a generic activity intent (meaning it can be in any application).

Upvotes: 1

Related Questions