notification in second activity not working - android

i have posted a question about this an the solution was perfect , but when i merge it with my code there is no result , the app dont make the notification ,
like here i put " mo " as the start activity .
here is my code :
public class mo extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mo);
}
and the second was which has the alarm manager :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Calendar cal = Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
// Settings calendar for 01/05/2016 12:33:00 AM
calendar.set(Calendar.YEAR, 2016);
calendar.set(Calendar.MONTH, 2); // January has value 0
calendar.set(Calendar.DAY_OF_MONTH, 15);
calendar.set(Calendar.HOUR_OF_DAY, 1);
calendar.set(Calendar.MINUTE, 18);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.PM );
// cal.add(Calendar.SECOND, 15);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast);
The broadcastReceiver :
public class AlarmReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, NotificationActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(NotificationActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle(" App Notification")
.setContentText("New Notification From Demmmmo App..")
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
}
The notificationActivity :
public class NotificationActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_activity);
}
}
and also here the manifest :
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
</activity>
<activity android:name=".NotificationActivity" />
<receiver android:name=".AlarmReceiver" >
<intent-filter>
<action android:name="android.media.action.DISPLAY_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name=".mo"
android:label="#string/title_activity_mo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.media.action.IMAGE_CAPTURE" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.media.action.IMAGE_CAPTURE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

Related

Getting NULL in context while auto restart with BROADCAST RECEIVER in Android app

I am developing android application which need auto restart when restart device for that I am using broadcast receiver with action BOOT_COMPLETED.
Broadcast receiver is receiving message when I am restarting device but in restart method I want to start main activity for that I used Intent but in onReceive method of receiver I am getting null context so I am unable to restart main Activity.
Below is code for that.
MainActivity.java
private Object activity;
private TextView tvImeiNum;
private BroadcastReceiver rebootreceiver;
private IntentFilter filter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvImeiNum = (TextView)findViewById(R.id.tv_imeinum);
filter = new IntentFilter();
filter.addAction(Intent.ACTION_BOOT_COMPLETED);
filter.addAction(Intent.ACTION_REBOOT);
rebootreceiver = new BootUpReceiver(MainActivity.this);
LocalBroadcastManager.getInstance(this).registerReceiver(rebootreceiver,
filter);//registering receiver
generateUniqueCode();
}
BootupReceiver.java
public class BootUpReceiver extends BroadcastReceiver {
MainActivity ma;
public BootUpReceiver(MainActivity maContext){
ma=maContext;
}
public BootUpReceiver(){
}
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Log.d("TAG REBOOT", "onReceive: " + intent);
Log.d("Reboot complete", "connection");
GlobalTool.restartApplication(context);
}
}
}
GlobalTool.java
public class GlobalTool {
#RequiresApi(api = Build.VERSION_CODES.M)
public static void restartApplication(Context context) {
Log.d("IN App restart:", "");
Log.d("TAG", "restartApplication: ");
if(context != null)
{
Log.d("TAG NULL", "restartApplication: ");
Intent mainIntent = new Intent(context, MainActivity.class);
AlarmManager alarmMgr = (AlarmManager)
context.getSystemService(ALARM_SERVICE);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(mainIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
long alarmTime = System.currentTimeMillis() + (1 * 1000);
alarmMgr.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
Log.d("TAG", "restartApplication: 111");
Log.d("TAG", "restartApplication: 111");
}
else {
Intent mainIntent = new Intent(context, MainActivity.class);
AlarmManager alarmMgr = (AlarmManager)
context.getSystemService(ALARM_SERVICE);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(mainIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
long alarmTime = System.currentTimeMillis() + (1 * 1000);
alarmMgr.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
Log.d("TAG", "restartApplication: 111");
Log.d("TAG", "restartApplication: 111");
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.kioskappdemo">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name="KioskApplication"
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.KioskAppDemo"
tools:targetApi="31"
tools:ignore="Instantiatable">
<service
android:name=".RebootService"
android:enabled="true"
android:exported="true"></service>
<activity
android:name=".MainActivity"
android:exported="true"
android:screenOrientation="landscape"
android:theme="#style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".Activity.BootUpReceiver"
android:enabled="true"
android:exported="true">
<intent-filter >
<action
android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.REBOOT" />
<action
android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.reboot.test" />
<action
android:name="android.intent.action.TIMEZONE_CHANGED" />
<action
android:name="android.intent.action.DATE_CHANGED" />
</intent-filter>
</receiver>
</application>
</manifest>
Some you code improvements:
Don't put context to Brodcast Receiver constructor. Use the Context from onReceive method
public class BootUpReceiver extends BroadcastReceiver {
public BootUpReceiver(){
//TODO log
}
...
You should not use LocalBroadcastManager::registerReceiver call. If Broadcast receiver is added to AndroidManifest then it will be started by the system.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name=".activity.BootUpReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Don't use CamelCase for package names (use .activity.BootUpReceiver insetad .Activity.BootUpReceiver)

Unable to show notification when app is closed

I am trying to show notification at specific date I am using alarm manager and broadcast receiver to show notifications but the problem is notification works only when app is open and when app is closed notification does not show. Below is my code:
Reminder.java
public class Reminder extends AppCompatActivity {
long reminderDateTimeInMilliseconds = 000;
Button but;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminder);
but = findViewById(R.id.but);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createNotifyChannel();
Intent intent = new Intent(Reminder.this,ReminderBroadcast.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Reminder.this,0,intent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Calendar calendarToSchedule = Calendar.getInstance();
calendarToSchedule.setTimeInMillis(System.currentTimeMillis());
calendarToSchedule.clear();
//.Set(Year, Month, Day, Hour, Minutes, Seconds);
calendarToSchedule.set(2020, 8, 20, 19, 12, 0);
reminderDateTimeInMilliseconds = calendarToSchedule.getTimeInMillis();
alarmManager.setExact(AlarmManager.RTC_WAKEUP,reminderDateTimeInMilliseconds,pendingIntent);
}
});
}
private void createNotifyChannel(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
CharSequence name = "ReminChannel";
String desc = "This is my channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("mynotif",name,importance);
channel.setDescription(desc);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
ReminderBroadcast.java
public class ReminderBroadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder notif = new NotificationCompat.Builder(context,"mynotif")
.setContentTitle("Appointment reminder")
.setContentText("Hello there")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat manager = NotificationManagerCompat.from(context);
manager.notify(200,notif.build());
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.firstapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Reminder">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ThirdActivity" />
<activity android:name=".SecondActivity" />
<activity android:name=".MainActivity">
</activity>
<receiver android:name=".ReminderBroadcast"/>
</application>
</manifest>
What am I doing wrong?

How to set an alarm even when app is killed? (Android 9)

I have implemented a simple app in order to set an alarm (e.g. 7.00pm) every minute. When running this app at emulator, which android version is 8.1, it runs successfully in background even the app is killed. But when running the same app at my android 9 when app is killed it stops running in background.
public class MainActivity extends AppCompatActivity {
private Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this;
ComponentName receiver = new ComponentName(this, AlarmReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Intent alarm = new Intent(this.context, AlarmReceiver.class);
alarm.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
boolean alarmRunning = (PendingIntent.getBroadcast(this.context, 0, alarm, PendingIntent.FLAG_NO_CREATE) != null);
if(alarmRunning == false) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.context, 0, alarm, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime(), 60000, pendingIntent);
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="com.android.alarm.permission.FLAG_ACTIVITY_NEW_TASK" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".BackgroundService" />
<receiver android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true"
android:label="RestartServiceWhenStopped">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.TIME_SET" />
</intent-filter>
</receiver>
</application>
AlertReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
private static int minutes=0;
#Override
public void onReceive(Context context, Intent intent) {
Log.e("Service",String.valueOf(Calendar.getInstance().getTimeInMillis()));
intent = new Intent(AlarmClock.ACTION_SET_ALARM);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
minutes++;
intent.putExtra(AlarmClock.EXTRA_HOUR, 7);
intent.putExtra(AlarmClock.EXTRA_MINUTES, minutes);
context.startActivity(intent);
/*Intent background = new Intent(context, BackgroundService.class);
context.startService(background);*/
}
Do you have any idea how to fix this? Thank you in advance!

I can't controll the setting time in AlarmManager when restart APP

I want to set the notification for custom time , i take a reference from http://www.singhajit.com/schedule-local-notification-in-android/
I add some manifest code which is for when the device reboot.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="internalOnly"
package="com.example.user.testcounddown">
<!-- for reboot---------------------- -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.media.action.DISPLAY_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
<!-- for reboot--------------------- -->
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
The problem is when i reboot my device , the notification shows immediately , not after about 300esc cal.add(Calendar.SECOND, 300);
here is my MainActivity code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 300);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);//for API<=18
}
my receiver code:
public class AlarmReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle("Demo App Notification")
.setContentText("New Notification From Demo App..")
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
}
How do i let the notification shows in the right time when device reboot ?
cal.add(Calendar.SECOND, 300);
Any help would be greatly appreciated,thanks.

Alarmmanager for a notification doesn't work

I've got an alarmmanager to display a notification at a certain date and time.
I schedule a notification like this:
private void scheduleNotification(String title, String text, String subtext, long futureInMillis) {
Intent notificationIntent = new Intent(getActivity(), NotificationPublisher.class);
notificationIntent.putExtra("title", title);
notificationIntent.putExtra("text", text);
notificationIntent.putExtra("subtext", text);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
}
The broadcastreceiver:
public class NotificationPublisher extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.v("receiver", "notification received");
String title = intent.getStringExtra("title");
String text = intent.getStringExtra("text");
String subtext = intent.getStringExtra("subtext");
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSmallIcon(R.drawable.checkbox_on_24dp);
builder.setContentTitle(title);
builder.setContentText(text);
builder.setSubText(subtext);
builder.setSound(uri);
builder.setVibrate(new long[]{1000});
Notification not = builder.build();
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, not);
}
}
The manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.christophbielen.simplytodo">
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name=".data.DataProvider"
android:authorities="com.christophbielen.simplytodo" />
<activity
android:name=".DetailActivity"
android:label="#string/title_activity_detail"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.christophbielen.simplytodo.MainActivity" />
</activity>
<activity
android:name=".ListCategoriesActivity"
android:label="#string/title_activity_list_categories"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.christophbielen.simplytodo.MainActivity" />
</activity>
<activity
android:name=".AddListActivity"
android:label="#string/title_activity_add_list"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.christophbielen.simplytodo.MainActivity" />
</activity>
<activity
android:name=".EditListActivity"
android:label="#string/title_activity_edit_list"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.christophbielen.simplytodo.MainActivity" />
</activity>
<activity
android:name=".AddItemActivity"
android:label="#string/title_activity_add_item"
android:theme="#style/AppTheme.NoActionBar">
<!--<meta-data-->
<!--android:name="android.support.PARENT_ACTIVITY"-->
<!--android:value="com.christophbielen.simplytodo.MainActivity" />-->
</activity>
<activity
android:name=".EditItemActivity"
android:label="#string/title_activity_edit_item"
android:theme="#style/AppTheme.NoActionBar">
</activity>
<receiver android:name=".NotificationPublisher" />
</application>
And I call schedule Notification like this:
int year = Integer.parseInt(date.substring(0,4));
int month = Integer.parseInt(date.substring(4,6))-1;
int day = Integer.parseInt(date.substring(6,8));
int hour = Integer.parseInt(time.substring(0,2));
int minute = Integer.parseInt(time.substring(2));
Calendar cal=Calendar.getInstance();
cal.set(Calendar.MONTH,month);
cal.set(Calendar.YEAR,year);
cal.set(Calendar.DAY_OF_MONTH,day);
cal.set(Calendar.HOUR_OF_DAY,hour);
cal.set(Calendar.MINUTE,minute);
cal.set(Calendar.SECOND,0);
String subcontent = catName + " due on: " + year + "/" + month+1 + "/" + day + " " + hour + ":" + minute;
scheduleNotification("To do!", shoppingListName, subcontent, cal.getTimeInMillis());
When I schedule a notification it seems to work but the receiver never receives anything.
I looked everywhere for an answer and I can't figure out why the alarmmanager doesn't work.
Thanks
Please Use -
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, (System.currentTimeMillis() + futureInMillis, pendingIntent);
instead of -
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
When you schedule an alarm with ELAPSED_REALTIME_WAKEUP, you must use SystemClock.elapsedRealtime() as the base offset for the time.
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + futureInMillis, pendingIntent);
DroidFiddle example: https://droidfiddle.net/vwkttsz/1

Categories

Resources