In my app, users have to fill in 9 different times with timepickers. These times are stored in a SQLite database. I want that users get a status bar notification when one of the times has been reached.
Example:
Users fill the first time in at 09:00.
If it is 09:00 in real time, users need to get a statusbar notification. I want that they don't need to start the app, it has to be automatic.
After spending more then 4 hours today at stackoverflow and other websites, I didn't succeed. I have tried the following code, but I didn't succeed. The notification is now set to 12:00. When the time reached 12:00, I didn't get a message.
MainActivity:
public class MainActivity extends Activity {
AlarmManager am;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent alarmIntent = new Intent(this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5000, pendingIntent); } }
TimeAlarm.class:
public class TimeAlarm extends BroadcastReceiver {
NotificationManager nm;
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, NotificationService.class);
context.startService(startServiceIntent);
}
}
NotificationService.class:
public class NotificationService extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleIntent(intent);
return START_NOT_STICKY;
}
private NotificationManager nm;
private WakeLock mWakeLock;
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onDestroy() {
super.onDestroy();
mWakeLock.release();
}
private void showNotification() {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Test")
.setContentText("Test message");
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
private class PollTask extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
showNotification();
return null;
}
#Override
protected void onPostExecute(Void result) {
stopSelf();
}
}
private void handleIntent(Intent intent) {
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"NotificationsService");
mWakeLock.acquire();
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
if (!cm.getBackgroundDataSetting()) {
stopSelf();
return;
}
new PollTask().execute();
}
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission
android:name="com.test.app.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- This app has permission to register and receive data message. -->
<uses-permission
android:name="com.google.android.c2dm.permission.RECEIVE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<activity
android:name="com.test.app.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.test.app.TimeAlarm">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
Try changing START_NOT_STICKY to START_STICKY;. If your service isn't sticky android does not try to restart it if it has to be killed to make room for other apps in memory.
Related
In Android Oreo, I'm trying to schedule notifications at known future times. I wrote a small piece of code to test if I can schedule a notification in an hour when the app might be closed. When I try to do this, nothing happens. I really appreciate the help.
public class MainActivity extends AppCompatActivity {
private NotificationManager manager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NotificationChannel notificationChannel = new NotificationChannel("default",
"primary", NotificationManager.IMPORTANCE_DEFAULT);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(notificationChannel);
Intent notifyIntent = new Intent(this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
long milisInFuture = 1000 * 60 * 60;
alarmManager.set(AlarmManager.RTC_WAKEUP, milisInFuture, pendingIntent);
}
public class MyNewIntentService extends JobIntentService {
#Override
protected void onHandleWork(#NonNull Intent intent) {
Notification notification = new Notification.Builder(getApplicationContext(), "default")
.setContentTitle("title")
.setContentText("body")
.setSmallIcon(android.R.drawable.stat_notify_chat)
.setAutoCancel(true)
.build();
manager.notify(123, notification);
}
}
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent(context, MyNewIntentService.class);
context.startService(intent1);
}
}
}
Here is the manifest if that helps.
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity$MyNewIntentService"
android:permission="android.permission.BIND_JOB_SERVICE"></service>
</application>
</manifest>
UPDATE:
Don't forget to add the receiver in the manifest xml file.
For this question:
Add in AndroidManifest.xml inside application tag:
<receiver android:name=".MyReceiver" />
Example:
I am keeping this example, it updates the notification from Broadcast Receiver class.
Create a broadcast receiver class as shown, (Don't forget to add the receiver in the manifest xml file)
public class NotificationUpdate extends BroadcastReceiver {
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
public void onReceive(Context context, Intent intent) {
//NEED A RESCHEDULE?
updateNotification(context);
}
#RequiresApi(api = Build.VERSION_CODES.O)
private void updateNotification(Context context){
Notification notification = new Notification.Builder(context.getApplicationContext(), "default")
.setContentTitle("title")
.setContentText("body")
.setSmallIcon(android.R.drawable.stat_notify_chat)
.setAutoCancel(true)
.build();
NotificationManager manager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
if(manager!=null) manager.notify(123, notification);
}
}
Example Call from an Activity:
public class MainActivity extends AppCompatActivity {
final long intervalPeriod=60*1000;
AlarmManager mAlarmManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NotificationChannel notificationChannel = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel("default",
"primary", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (manager != null) manager.createNotificationChannel(notificationChannel);
mAlarmManager=(AlarmManager)getApplicationContext().getSystemService(ALARM_SERVICE);
PendingIntent intent=PendingIntent.getBroadcast(getApplicationContext(),1234,
new Intent(getApplicationContext(),NotificationUpdate.class),0);
mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+intervalPeriod, intent);
}
}
I am working on an App in which i want to set custom alarm.but after setting a time on alarm nothing happens. i am sharing my code. Please help and guide me if something is missing in my code.
In my MainActivity.java
#Override
protected Dialog onCreateDialog(int id) {
if (id == DIALOG_ID) {
return new TimePickerDialog(MainActivity.this, kTimePickerListener, hour, min, false);
}
return null;
}
protected TimePickerDialog.OnTimeSetListener kTimePickerListener =
new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker timePicker, int hourofday, int minutes) {
hour = hourofday;
min = minutes;
Calendar calendar = Calendar.getInstance();
// set selected time from timepicker to calendar
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
Intent myIntent = new Intent(MainActivity.this, ServiceManager.class);
myIntent.putExtra("reqcode", 11);
// A PendingIntent specifies an action to take in the
// future
PendingIntent mPendingIntent = PendingIntent.getBroadcast(MainActivity.this, 11, myIntent, 0);
// set alarm time
alarmManager = (AlarmManager) MainActivity.this
.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), mPendingIntent);
}
};
In My ServiceManager.class
public class ServiceManager extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
int Noti_code = intent.getIntExtra("reqcode",-1);
Intent myIntent = new Intent(context, NotificationService.class);
myIntent.putExtra("reqcode", Noti_code);
context.startService(myIntent);
}
}
In My NotificationService.class
public class NotificationService extends Service {
private NotificationManager mManager;
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
#SuppressWarnings({"static-access", "deprecation"})
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
int Noti_Code = intent.getIntExtra("reqcode",-2);
if (Noti_Code == 11) {
mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
ShowNotification(getApplicationContext(), mManager);
}
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
In my Manifest.xml
<uses-permission android:name="android.permission.WAKE_LOCK" />
<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>
<activity android:name=".Activity_detail" />
<activity android:name=".History" />
<service
android:name=".NotificationService"
android:enabled="true"
android:exported="true"
android:stopWithTask="false"/>
<receiver android:name=".ServiceManager"
android:process=":remote"/>
</application>
I am checking through debugging broadcast receiver doesn't called. Please help me if i am missing any thing.
Also i want to know how many pending intent can be fire at a time and will all works one by one?
Thanks.
Refrence links:
AlarmManager wont fire pending intent
http://code4reference.com/2012/07/tutorial-on-android-alarmmanager/
BroadcastReceiver and AlarmManager not working with
AlarmManager is not working after app is closed? - Android
You have your BroadcastReceiver running in another process. If you are setting breakpoints with a debugger you will miss this. If you don't need your BroadcastReceiver running in a separate process, remove the
android:process=":remote"
declaration from the manifest.
See the documentation about how the android:process specifier works.
I have an AlarmReceiver with an AlarmService and my problem is, if I set the AlertDate about 30min away from the current time, it just does it's job and calls the Alert-function. But if I set it about 5hours away, it won't call the function, no matter if I do it at night (not using my phone) or at day (while my phone is in use).
So my question is, why can't I set the AlertDate in the "far" future? Is the process killed or is there anything strange happening? And in particular: How can I fix this problem??
Basically I have the arrangement from here: Android Alarm Clock Tutorial
So I just point out my main lines in the code:
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="apm.smoothalert" >
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
...
<application
...
<activity
android:name=".main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".AlarmService"
android:enabled="true" />
<receiver android:name=".AlarmReceiver" />
</application>
main:
public class main extends ActionBarActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
PowerManager powman = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powman.newWakeLock(PowerManager.FULL_WAKE_LOCK,"MyWakelockTag");
wakeLock.acquire();
...
}
AlarmManager alarmManager;
private PendingIntent pendingIntent;
private void smoothAlertOn ()
{
Date AlertDate = ... ; //Here I select the Alert date with a Timepicker
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent myIntent = new Intent(main.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(main.this, 0, myIntent, 0);
alarmManager.set(AlarmManager.RTC, AlertDate.getTime(), pendingIntent);
}
public static void SmoothAlert(final Context context)
{
//Here i do the Alert with Notifications etc.
}
}
AlarmReceiver:
public class AlarmReceiver extends WakefulBroadcastReceiver
{
static Context Alarmcontext;
#Override
public void onReceive(final Context context, Intent intent)
{
Alarmcontext = context;
ComponentName comp = new ComponentName(context.getPackageName(),AlarmService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
AlarmService:
public class AlarmService extends IntentService
{
public AlarmService()
{
super("AlarmService");
}
#Override
public void onCreate()
{
super.onCreate();
}
#Override
public void onHandleIntent(Intent intent)
{
main.SmoothAlert(AlarmReceiver.Alarmcontext);
}
}
I am trying to use BroadcastReceiver and AlarmManager to set a one-shot alarm. I have no idea why isn`t it working. What am I doing wrong? I have no exceptions, no logs about, no suggestions from IDE, everything seems fine, but onReceive method in my BroadcastReceiver is never called.
public void setAlarm(Context mContext) {
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(mContext, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 505151550 ,intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 20);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
Log.d("alarm",cal.getTime().toString());
}
I am calling it from the activity:
new MyManager().setAlarm(this);
but I have also tried: new MyManager().setAlarm(MyActivity.this);
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("alarm","got into alarm receiver");
}}
in AndroidManifest.xml :
<receiver android:name="com.example.AlarmReceiver"></receiver>
but I have also tried
<receiver android:name=".AlarmReceiver"></receiver>
try like this: it will fire alarm after 30 second.
public class TestAlarmManager extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setAlarm(getApplicationContext());
}
public void setAlarm(Context mContext) {
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (30 * 1000), pendingIntent);
Toast.makeText(this, "Alarm set in " + 30 + " seconds",Toast.LENGTH_LONG).show();
}
}
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent arg1) {
Log.e("alarm","got into alarm receiver");
Toast.makeText(context, "Alarm Started..", Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.think.androidteststackoverflow"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher_3"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.think.androidteststackoverflow.TestAlarmManager"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmReceiver"></receiver>
</application>
</manifest>
Why my AlarmManager for service update doesn't work? this is my code:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService();
}
// Method to start the service
public void startService() {
startService(new Intent(getBaseContext(), MyService.class));
}
// Method to stop the service
public void stopService() {
stopService(new Intent(getBaseContext(), MyService.class));
}
Service class:
public class MyService extends Service {
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
#Override
public void onCreate() {
poruka();
super.onCreate();
}
public void poruka(){
Toast.makeText(this, "OnCreate work!", Toast.LENGTH_LONG).show();
}
}
BroadcastReceiver and AlarmManager:
public class ServiceBroadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, MyService.class));
Intent alarmIntent = new Intent(context, ServiceBroadcast.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, Calendar.getInstance().getTimeInMillis(), 30*1000, pendingIntent);
}
}
And Permissions:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="me.example.nservice.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>
<service android:name=".MyService" android:enabled="true"
android:label="Servisv1" />
<receiver android:name="me.example.rqservice.ServiceBroadcast" android:process=":remote">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
I receive message "OnCreate work!" and "Service Started" only first time when i open app. Why update doesn't work every 30 sec?
To start a service every 30 seconds :
Inside onCreate() :
Here MyService is the name of the Service.
Intent myService = new Intent(this, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(
this, 0, myService, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), 30*1000,pendingIntent);
This'll start your service every 30 seconds. However, if the service is already running, which might be the case once it has been started the first time, from next time onwards the call will directly go to onStartCommand() method of the Service and not onCreate().
This is all that you need to do. However, if you want to make sure that the AlarmManager keeps restarting the service even after a phone is restarted, you'll need to add a BroadcastReceiver for Boot.