5

i want to start an alram when the device gets boot, for that i have done following stuff

1) User permission

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

2) add receiver with intent action in manifest file

 <receiver
            android:name=".sms.BootReceiver"
            android:enabled="true" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" >
                </action>

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

3) source

public class BootReceiver extends BroadcastReceiver {

    private AlarmManager dayAlarmMgr;
    private PendingIntent dayAlarmIntent;
    private NotificationManager mNotificationManager;
    private NotificationCompat.Builder builder;
    private Context context;
    public static final int NOTIFICATION_ID = 2;

    @Override
    public void onReceive(Context context, Intent intent) {

         if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
                    Toast.makeText(context, "FIRED BOOT COMPLETE" , Toast.LENGTH_LONG).show();
           }
    }
}

above code is working in genymotion but not on a real device

JafarKhQ
  • 8,676
  • 3
  • 35
  • 45
Hunt
  • 8,215
  • 28
  • 116
  • 256

5 Answers5

4

thanks for helping out but finally i was able to make it work android.intent.action.BOOT_COMPLETED by setting android:installLocation="internalOnly" as boot complete fires for application those are stored in internal memory.

Hunt
  • 8,215
  • 28
  • 116
  • 256
  • Worked for me . check the link for the details for why to use android:installLocation="internalOnly" https://developer.android.com/guide/topics/data/install-location#ShouldNot – Jordon Sep 02 '22 at 15:54
1

The problem is android:name=".sms.BootReceiver", it should be android:name=".BootReceiver". But some devices don't catch BOOT_COMPLETED. Your intent-filter should be like following:

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

And don't forget to edit your source:

if ((intent.getAction().equals("android.intent.action.BOOT_COMPLETED") 
   || intent.getAction().equals("android.intent.action.QUICKBOOT_POWERON")
   || intent.getAction().equals("com.htc.intent.action.QUICKBOOT_POWERON"))){
      Toast.makeText(context, "FIRED BOOT COMPLETE" , Toast.LENGTH_LONG).show();
}
Aldo
  • 373
  • 3
  • 7
0
  android:name=".sms.BootReceiver"

instead of that use

android:name=".BootReceiver"

Or

android:name="complete.packagename.sms.BootReceiver"

Hope this will solve your issue.

khurram
  • 1,362
  • 13
  • 24
  • i have tried your solution , but this thing gets called only when my device automatically gets rebooted i.e. when my device goes out of battery and switched off – Hunt Jan 24 '15 at 18:59
0

Make Sure you have atleat one activity present in your Application.From Android 3.1, BroadcastReceiver will not work until the user has manually launched an activity, This is for provide security . once the user runs the app for the first time then your BroadcastReceiver will run always except it does not Force Stop it. Once activity launch at first time your broadcast receiver will run even after reboot your deice.

On emulator it is working- It may be because it is running on lower version than 3.1 and real device run on higher than 3.1

PPD
  • 5,660
  • 12
  • 52
  • 86
-1
<receiver android:name="your.package.example.BootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>