How to start activity from broadcast from Download manager?

575 views Asked by At

I want to resume if posible, start new activity otherwise from Broadcast receiver, when user click on notification from Download Manager.

Manifest

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
    <uses-permission android:name="com.sec.android.provider.badge.permission.READ" />
    <uses-permission android:name="com.sec.android.provider.badge.permission.WRITE" />
    <uses-permission android:name="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS" />
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

... 

     <receiver
                android:name=".rest.receiver.DownloadReceiver"
                android:enabled="true"
                android:exported="true">
                <intent-filter>
                    <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
                    <action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED"/>
                </intent-filter>
            </receiver>

Broadcast Reciever

private final static String TAG = "DownloadManagerStatus";


    @Override
    public void onReceive(Context context, Intent intent){
        String action = intent.getAction();
        Log.i(TAG, "onReceive " + action);
        if(action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
        context.startService(new Intent(context, DownloadCheckerService.class));
        else if(action.equals(DownloadManager.ACTION_NOTIFICATION_CLICKED)){
            Intent i = new Intent(context,MainActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(i);
        }
    }

If I click on notification, when my application is foregraound or on background - the correct activity is resumed.

If I click on notification, when my application is terminated or was not launched(like after restart) - broad cast receiver does not receive any intent.

1

There are 1 answers

1
narancs On

I think, the problem, when you terminate your process you also kill the broadcast receiver. You have to create a separate process for you broadcast receiver. Should I use android: process =":remote" in my receiver?