Android : Unable to start an activity automatically through "BroadcastReceiver" when device reboots

on Friday, July 11, 2014


Just got through Broadcast Receiver and tried to implement it on my app. I want the MainActivity of my app to start automatically when my phone reboots. Now after implementing Broadcast Receiver whole code and setting everything in AndroidMenifest when I restart my phone, it gives an Application error "app_name has stop working".


This is my BootBroadcastReceiver.java class :



public class BootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Do your work related to alarm manager
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, MainActivity.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(serviceIntent);
}
}
}


This is my AndroidMenfest.xml :



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ialarm"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >


<service android:name=".MainActivity" android:label="Main Activity" android:exported="false">
<intent-filter>
<action android:name="com.example.ialarm.MainActivity" />
</intent-filter>
</service>

<receiver
android:name=".BootBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:label="BootBroadcastReceiver">

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

</receiver>

<activity
android:name="com.example.ialarm.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="set_alarm_activity"
android:windowSoftInputMode="adjustNothing"
/>
</application>

</manifest>


I feel there can be some problem with AndroidMenifest, but i set everything according to this stackoverflow question's answers How to start an Application on startup?. I just want to start my MainActivity when my device reboots, please help. Apologies for any mistake.Thanks


0 comments:

Post a Comment