Pramod Yadav
Pramod Yadav

Reputation: 2326

can i start another application using the broadcast receiver?

i just wanted to know i have created an application and installed it on my device.Is it possible to start this application by using only a broadcast receiver if so how can i do this?

Upvotes: 1

Views: 4018

Answers (3)

mark.kedzierski
mark.kedzierski

Reputation: 663

When you send the broadcast you need to add a flag to start a package which isn't already running.

getContext().sendBroadcast(new Intent("MY_ACTION").setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES), null);

Upvotes: 0

Chintan Rathod
Chintan Rathod

Reputation: 26034

Use following code to open activity while your application is not running.

Intent myIntent = new Intent(context.getApplicationContext(), YourClassActivity.class);
Bundle bundle = new Bundle();
myIntent.putExtras(bundle);
myIntent.addFlags(
      Intent.FLAG_ACTIVITY_NEW_TASK
    | Intent.FLAG_ACTIVITY_CLEAR_TOP
    | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.getApplicationContext().startActivity(myIntent);

you can pass any data inside bundle like

bundle.putString("title", "from receiver");

Upvotes: 1

Yaroslav Mytkalyk
Yaroslav Mytkalyk

Reputation: 17105

If BroadcastReceiver is listening then the Application is "running" also. If you mean to open Activity, then

@Override
public void onReceive(Context context, Intent intent) {
    context.startActivity(new Intent(context, YourActivity.class));
}

You need to register the receiver statically in manifest (from doc)

You can either dynamically register an instance of this class with Context.registerReceiver() or statically publish an implementation through the <receiver> tag in your AndroidManifest.xml. You should also specify the intent action you cant to catch

<receiver android:name=".YourReceiver"
    android:exported="true"
    android:enabled="true" >
    <intent-filter>
       <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
    </intent-filter>
</receiver>

Upvotes: 0

Related Questions