-2

My app work when my android is not restart but when I turn off my Android the app not working despite I add BOOT_COMPLETED.

I have looked for similar questions but all of them work just as I do, I do not know what is wrong

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.proyect.d.alarm">

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


    <service android:name=".BootService" />

    <receiver
        android:name=".RestartAlarmsReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <receiver
        android:name=".MyAlarmReceiver"
        android:process=":remote" />

</application>

RestartAlarmsReciver

public class RestartAlarmsReceiver extends BroadcastReceiver {

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

        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {


            Intent i = new Intent(context, BootService.class);
            ComponentName service = context.startService(i);
        }

    }
}

BootService: it's equals that my main AlarmService

public class BootService extends IntentService {
    public BootService(String name) {
        super(name);
    }

    private NotificationManager notificationManager;
    private final int NOTIFICATION_ID = 1010;
    private AdminSQLiteOpenHelper admin;
    private Cursor fila;
    private SQLiteDatabase bd;
    private String alarm, descrip, title;

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Calendar calenda = Calendar.getInstance();
        int hour, min, day, m, year;
        String cadenaF, cadenaH, date_system, time_system;

        day = calenda.get(Calendar.DAY_OF_MONTH);
        m = calenda.get(Calendar.MONTH) + 1;
        year = calenda.get(Calendar.YEAR);
        hour = calenda.get(Calendar.HOUR_OF_DAY);
        min = calenda.get(Calendar.MINUTE);
        date_system = m + "-" + day + "-" + year + " ";
        time_system = hour + ":" + min;
        admin = new AdminSQLiteOpenHelper(getApplicationContext(), vars.bd, null, vars.version);
        bd = admin.getWritableDatabase();

        if (bd != null) {
            fila = bd.rawQuery("SELECT * FROM alarma WHERE datea='" + date_system + "' AND timea= '" + time_system + "'", null);
            if (fila.moveToFirst()) {
                alarm = fila.getString(0);
                title = fila.getString(1);
                descrip = fila.getString(2);
                triggerNotification(getApplicationContext(), title + "\n" + descrip);
            }
        }
        bd.close();
    }

    private void triggerNotification(Context contexto, String t) {
        Intent notificationIntent = new Intent(contexto, MainActivity.class);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(contexto, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        long[] pattern = new long[]{2000, 1000, 2000};

        NotificationCompat.Builder builder = new NotificationCompat.Builder(contexto);
        builder.setContentIntent(contentIntent)

                .setTicker("")
                .setContentTitle("alarm ")
                .setContentTitle("")
                .setContentText(t)
                .setContentInfo("Info")
                .setLargeIcon(BitmapFactory.decodeResource(contexto.getResources(), R.drawable.ic_launcher_background))
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setAutoCancel(true) 
                .setSound(defaultSound)
                .setVibrate(pattern);

        Notification notificacion = new NotificationCompat.BigTextStyle(builder)
                .bigText(t)
                .setBigContentTitle("example")
                .setSummaryText("more example")
                .build();

        notificationManager = (NotificationManager) contexto.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, notificacion);
    }

}

Thanks

Jeorge
  • 73
  • 1
  • 7

1 Answers1

0

Can you change your receiver to add export and category?

<receiver android:name=".RestartAlarmsReceiver" android:enabled="true" android:exported="true">
    <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED" />
         <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>            
</receiver>

As per official documentation

android:exported

Whether or not the broadcast receiver can receive messages from sources outside its application — "true" if it can, and "false" if not. If "false", the only messages the broadcast receiver can receive are those sent by components of the same application or applications with the same user ID. The default value depends on whether the broadcast receiver contains intent filters. The absence of any filters means that it can be invoked only by Intent objects that specify its exact class name. This implies that the receiver is intended only for application-internal use (since others would not normally know the class name). So in this case, the default value is "false". On the other hand, the presence of at least one filter implies that the broadcast receiver is intended to receive intents broadcast by the system or other applications, so the default value is "true".

This attribute is not the only way to limit a broadcast receiver's external exposure. You can also use a permission to limit the external entities that can send it messages (see the permission attribute).

Hope this will help