0

I am trying to launch the main activity (which is the only activity) named as "MainActivity" when the Android OS starts. The app is installed on the internal storage but it says Unfortunately, The app(Name of app) has stopped. I am using the Boot Receive code as below

My MainActivity code in .java file

public class BootUpReceiver extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent) {
            Intent i = new Intent(context, MainActivity.class);  
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);  
    }
}

AndroidManifest.xml

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

      <receiver android:enabled="true" android:name=".BootUpReceiver"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

    <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
     </receiver>
CodeAddiction
  • 89
  • 2
  • 12
  • try it after adding `` permission before application tag in AndroidManifest.xml and also add logcat result with question when app is crashing – ρяσѕρєя K Mar 08 '13 at 07:11
  • Thanks for quick reply, permissions are added already in Manifest file. Application is crashing when i restart the Android TV box, after 10 seconds it says app has stopped. – CodeAddiction Mar 08 '13 at 07:13
  • Pls remove this below line from reciever tag and add it above application tag in manifiest android:permission="android.permission.RECEIVE_BOOT_COMPLETED" – Nikhil Virupakshi Mar 08 '13 at 07:23
  • Did it, not working :( – CodeAddiction Mar 08 '13 at 07:34
  • have u get anything in the logcat ? – Shoshi Mar 08 '13 at 08:18
  • is your activity started after reboot? or not starting just crashing ? – Shoshi Mar 08 '13 at 08:23
  • Activity does not start at all, it directly after reboot says Unfortunately, app has stopped – CodeAddiction Mar 08 '13 at 08:53
  • can u post the logcat showing why your app crashes at all? – Royston Pinto Mar 08 '13 at 09:01
  • I am new to android, can you guide me on how can i get the logcat during app starts or what ? – CodeAddiction Mar 08 '13 at 09:26
  • Finally, did the right thing after searching some time, For some one who is stuck up here. What you have to do is register the onReceive function in a separate package, what i was getting the error in Logcat was Classnotfound as i placed the code in my MainActivity class and its package. As soon as i did changed the onReceive function to new package the application worked flawlessly. – CodeAddiction Mar 08 '13 at 11:41

2 Answers2

0

Finally, did the right thing after searching some time, For some one who is stuck up here. What you have to do is register the onReceive function in a separate package, what i was getting the error in Logcat was Classnotfound as i placed the code in my MainActivity class and its package. As soon as i did changed the onReceive function to new package the application worked flawlessly.

CodeAddiction
  • 89
  • 2
  • 12
  • When you add receiver in manifest add it with full namespace, i.e. package name and then broadcast class name. It should work now. – JAPS Oct 21 '13 at 10:52
-1

try this:

public class BootUpReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(action.equalsIgnoreCase("android.intent.action.BOOT_COMPLETED")) {
            Intent i = new Intent(context, MainActivity.class);  
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }  
    }
}

https://stackoverflow.com/a/11079306/6237295

Community
  • 1
  • 1