0

I've written following class to start my application activity Home.class but on device start up it shows error forced close.

public class MyBootRecvr extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, Home.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.addFlags(Intent.FLAG_FROM_BACKGROUND);
        context.startActivity(i);
        Toast.makeText(context, "Where is my KeyBoard", Toast.LENGTH_LONG)
                .show();
    }
}

Permissions and receiver tags in application.


<receiver
     android:name=".Home"
     android:enabled="true"
     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>

2 Answers2

0

If this is the only code in your application, it will never run. Android applications must have at least one subclass of Activity as a starting point for when you run the app. The Activity could should at a minimum look something like this:

class MyActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.my_layout); /* my_layout should be an XML layout in res/layout */
    }
}

Make sure the following code in is your AndroidManifest.xml file

<activity android:name="MyActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

If you create a new Android project in Eclipse, it will do this setting up for you. There are a lot of tutorials on both setting up a basic application in Android, and using Eclipse to do it for you.

Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221
Xono
  • 1,900
  • 17
  • 20
  • my app do have an activity thats what i am starting from intent –  Jun 15 '12 at 09:06
  • Intent i = new Intent(context, Home.class); –  Jun 15 '12 at 09:06
  • first be sure what you're posting –  Jun 15 '12 at 09:11
  • As far as I'm aware, the application must have an Activity to be launched when you open it. That activity should not be launched via an Intent, rather the intent filters you specify for it (as shown in my answer's AndroidManifest) caused it to be launched automatically. I'm not sure why you're trying to use an intent to manually launch it. – Xono Jun 15 '12 at 09:29
  • intent is used there when device start to start activity in background not i front –  Jun 15 '12 at 11:11
0

doing this solve the problem thanks to xono

<receiver
     android:name=".MyBootRecvr"
     android:enabled="true"
     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>