How to start activity in BroadcastReceiver when download is complete on Android

I use the code below so that my application can automatically start after the download is completed in 10 seconds:

public class BootActivity extends BroadcastReceiver { static final String ACTION = "android.intent.action.BOOT_COMPLETED"; public void onReceive(Context context, Intent intent) { if(intent.getAction().equals(ACTION)) { context.startService(new Intent(context, BootActivity.class)); try { Thread.sleep(10000); Intent newAct = new Intent(); newAct.setClass(BootActivity.this, NewActivity.class); startActivity( newAct ); } catch(Exception e) { e.printStackTrace(); } } } } 

But setClass and startActivity cannot be used here.
How can I change it to run it?

+4
source share
2 answers

May this help you ...

Create a class called AutoStart.class

 public class AutoStart extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){ Intent i = new Intent(context, SochActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } } } 

And in the manifest declare it.

 <receiver android:name=".AutoStart" android:enabled="true" android:exported="true" > <intent-filter android:priority="500" > <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> 
+6
source

Try this in the manifest file,

 <receiver android:name=".BootActivity"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> 

Make sure you also enable complete download permission.

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 
+4
source

Source: https://habr.com/ru/post/1490435/


All Articles