Search code examples
androidforceclosereminders

Why After a while my program is force closed?


My app work as well, But after 12 or 24 hours does not work because the program has been closed by the operating system.

How can I prevent the closure of the program by the operating system?

My phone model: Huawei G620S-L02

I am set alarm like this:

AlarmManager alarmManager = (AlarmManager) this.mContext.getSystemService(Context.ALARM_SERVICE);        
Intent intentAlarm = new Intent(this.mContext, AlarmReciever.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.mContext, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);

    long time = System.currentTimeMillis() + 25 * 60 * 60 * 1000
    alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);

and this is AlarmReciever:

    public class AlarmReciever extends BroadcastReceiver
{    
    @Override
    public void onReceive(Context context, Intent intent)
    {                
        PendingIntent pi = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher) // notification icon
                .setTicker(sticker)
                .setContentTitle("title") // title for notification
                .setContentText("text)   // message for notification
                .setAutoCancel(true)    // clear notification after click
                .setContentIntent(pi)
                .setLights(0xff00ff00, 300, 500)
                .setShowWhen(false);               

        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());
    }
}

and my Manifest is:

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

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


Solution

  • In some devices(like HUAWEI), Your app must be checked in Protected apps:

    Go to Settings => Protected apps => checked your apps