AlarmManager not call my service from Android wear app - java

I create an android wear app and I want to this app create notification some times (In this code I want to call my service every 15 minutes for creating notification)
When I register my service with AlramManager, I can see that task Created in OS (adb shell dumpsys alarm > dump.txt) but I can't see the notification or log to prove the service run. I test my service with startService and both notification and log worked correctly, So I don't know where is the problem. Here are the codes I use:
MyAlarmService.java
public class MyAlarmService extends Service {
#Override
public void onCreate() {
super.onCreate();
Log.d("From serivce","Service run once");
// Create notification
Context context = this;
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentTitle(context.getString(R.string.NotificationTitle))
.setSmallIcon(R.mipmap.ic_launcher_hafez)
.setContentText("Some text");
Notification secondPage =
new NotificationCompat.Builder(context)
.setContentTitle(context.getString(R.string.NotificationTitle))
.setContentText("Some text")
.build();
NotificationCompat.WearableExtender extender = new NotificationCompat.WearableExtender();
extender.addPage(secondPage);
builder.extend(extender);
NotificationManagerCompat mgr = NotificationManagerCompat.from(context);
int NotificationId = new Random().nextInt(5200);
mgr.notify(NotificationId, builder.build());
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
AndroidManifest.xml
...
<service android:name=".MyAlarmService" />
...
The code that register service in AlarmManager:
Context context = getActivity().getApplicationContext();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
Intent intent = new Intent(context, MyAlarmService.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_FIFTEEN_MINUTES, alarmIntent);
The content of dump.txt
Current Alarm Manager state:
...
Batch{91609e num=3 start=7228838 end=7233540 flgs=0x8}:
...
RTC_WAKEUP #0: Alarm{18a777f type 0 when 1467992333472 org.rashno.mypackagename}
tag=*walarm*:org.rashno.mypackagename/.MyAlarmService
type=0 whenElapsed=-6s582ms when=2016-07-08 20:08:53
window=+11m15s0ms repeatInterval=900000 count=0 flags=0x0
operation=PendingIntent{397c34c: PendingIntentRecord{de77f95 org.rashno.mypackagename broadcastIntent}}
...
Alarm Stats:
...
u0a57:org.rashno.mypackagename +9ms running, 1 wakeups:
+9ms 1 wakes 1 alarms, last -1m33s417ms:
*walarm*:org.rashno.mypackagename/.MyAlarmService
...
...

A PendingIntent for a service should be created using getService.
PendingIntent alarmIntent = PendingIntent.getService(context, 0, intent, 0);

Related

How to set multiple alarms firing distinct notifications for each in Android

I am trying to develop an application in which the user can create more than 1 alarms for the same time ,say, 09:48. And for this time, each alarm has to create its own notification which leads the user to an activity when the notification is clicked. The other alarms which were scheduled at the same time must keep ringing until their notifications are clicked. Now, I am able to create alarms for different times and get their corresponding notifications which lead to an activity in which the user can dismiss or snooze the alarm. However, when I schedule more than one alarm for the same time, only 1 alarm is ringing and the other one gets lost. I share my code pieces at the below.
public void schedule(Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmBroadcastReceiver.class);
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(context, alarmId, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
alarmManager.setExact(
AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
alarmPendingIntent
);
In my broadcastReceiver class:
private void startAlarmService(Context context, Intent intent) {
Intent intentService = new Intent(context, AlarmService.class);
Log.println(Log.DEBUG, "DEBUG", "************Alarm Broadcast Receiver**********");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intentService);
} else {
context.startService(intentService);
}
}
In my AlarmService :
#Override
public void onCreate() {
super.onCreate();
mediaPlayer = MediaPlayer.create(this, R.raw.alarm);
mediaPlayer.setLooping(true);
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent notificationIntent = new Intent(this, RingActivity.class);
int requestCode = new Random().nextInt(Integer.MAX_VALUE);
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, notificationIntent, 0);
//String alarmTitle = String.format("%s Alarm", intent.getStringExtra(TITLE));
int notificationId = new Random().nextInt(Integer.MAX_VALUE);
Notification notification = new NotificationCompat.Builder(this, String.valueOf(notificationId))
.setContentTitle("PASS1")
.setContentText("PASS2")
.setSmallIcon(R.drawable.pill)
.setContentIntent(pendingIntent)
.build();
mediaPlayer.start();
long[] pattern = { 0, 100, 1000 };
vibrator.vibrate(pattern, 0);
startForeground(1, notification);
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
mediaPlayer.stop();
vibrator.cancel();
}
in this code
startForeground(1, notification);
you are using 1 as the id.
If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.
So the second alarm notification with the same id (because you are using a fixed value of 1) is replacing the old one

Android: Run thread in background even when the app was closed by the user

I am trying to send a http request to my server in the background after I closed the app. But the thread is always being killed. I already tried Workmanager, AlarmManager and BackgroundService. I have been searching in the internet for solutions for the last weeks and I couldn't find any solutions working in newer API's and without a ForegroundService which has to display a notification while running.
Starting AlarmManager:
//NotificationAlarm is the class implementing BroadcastReceiver
Intent intent = new Intent(context, NotificationAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, ALARM_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(), 10000, pendingIntent);
Until about Android 6 the AlarmManager works to me. However in latest versions it keeps getting closed.
I used the following tutorial as template for BackgroundServices but it still did not work: https://medium.com/#raziaranisandhu/create-services-never-stop-in-android-b5dcfc5fb4b2
I'm Looking forward to an answer.
Did you ever tried ForegroundService?...
you can create class extended as service btw, just add the intent filter on the manifest.
Example from friend's class.
public class ForegroundService extends Service {
public static final String CHANNEL_ID = "ForegroundServiceChannel";
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
String input = intent.getStringExtra("inputExtra");
createNotificationChannel();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Foreground Service")
.setContentText(input)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
//do heavy work on a background thread
//stopSelf();
return START_NOT_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
And start it as
startForegroundService(context,ForegroundService.class);
There is a lot of options
(Foreground service will start a notification until its dead)

Alarm Manager not starting after 60 seconds

I'm trying to set my alarm manager working, just simple schedule, firing toast every minute, but it's not working, what's wrong with the code?
Main Activity :
public void klik(View view) {
startalarm();
}
public void startalarm(){
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent;
PendingIntent pendingIntent;
intent = new Intent(this, AlarmToastReciever.class);
pendingIntent = PendingIntent.getBroadcast(this,0,intent,0);
manager.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime()+3000,+60000,pendingIntent);
}
}
AlarmToastReciever class :
public class AlarmToastReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent){
Toast.makeText(context,"GOWNO", Toast.LENGTH_SHORT).show();
}
}
As stated in documentation
As of Android 4.4 (API Level 19), all repeating alarms are inexact. Note that while setInexactRepeating() is an improvement over setRepeating(), it can still overwhelm a server if every instance of an app hits the server around the same time. Therefore, for network requests, add some randomness to your alarms, as discussed above.
You can use "setInexactRepeating()" or set an exact one time alarm then set next alarm in On Receive method
Also make sure you added your receiver to the manifest file, between application tag, like
<receiver android:name=".AlarmToastReciever"
android:enabled="true">
<intent-filter>
</intent-filter>
</receiver>
Use this code to initialize alarm manager.
public void setupAlarm() {
final Calendar calNow = Calendar.getInstance();
final Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, calNow.get(Calendar.HOUR_OF_DAY));
calSet.set(Calendar.MINUTE, calNow.get(Calendar.MINUTE) + 1);
calSet.set(Calendar.SECOND, calNow.get(Calendar.SECOND));
final Intent intent = new Intent(this, UploadStarterReceiver.class);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1,
intent, 0);
final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(),
60 * 1000, pendingIntent);
}

Android registerReceiver Inside runing service that waiting for AlarmManager system msg

I have running service which includes broadcast receiver .
I need the broadcast receiver to get the message from system timer clock interrupt that Alarm Manager made.
The main idea behind this is that when user close the app the service will continue listening until time interrupt happens .
I dont know why its not working ): how to do it correct please ?
Main Activity :
service.registerRecvierForAlarm();
Intent intent = new Intent(Send.this, SmsReciverService.class);
intent.setAction("intent_myaction_alarm");
PendingIntent pintent = PendingIntent.getBroadcast(Send.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, time, pintent);
Service :
public void registerRecvierForAlarm(){
IntentFilter intentFilter = new IntentFilter("intent_myaction_alarm");
this.ReceiverAlarm = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("message recived");
}
};
this.registerReceiver(this.ReceiverAlarm,intentFilter);
}
SmsReciverService is service so you need to use getService() to create PendingIntent instead of getBroadcast(used for creating PendingIntent for BroadcastReceiver) to start Service using AlarmManager :
PendingIntent pintent = PendingIntent.getService(Send.this, 0, intent, 0);
OR
if you need to fire BroadcastReceiver using AlarmManager then creating Intent by using class name which is extending BroadcastReceiver as second parameter to Intent constructor :
Intent intent = new Intent(Send.this, SmsReciverBroadcast.class);
^^^^^^
intent.setAction("intent_myaction_alarm");
PendingIntent pintent = PendingIntent.getBroadcast(Send.this, 0, intent, 0);

Android notification programmed

I have build a simple app that show a notification when i click on a button. How can show a programmed notify?
The code that i call is:
Notification.Builder builder = new Notification.Builder(this)
.setTicker("Notifica")
.setSmallIcon(android.R.drawable.stat_notify_chat)
.setContentTitle("Notifica")
.setContentText("Hai una notifica!")
.setAutoCancel(true)
.setContentIntent(PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0));
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify("interstitial_tag", 1, builder.build());
You can use AlarmManager in bundle with BroadcastReceiver.
At first you must create pending intent and register it with AlarmManager.set somewhere.
And then create your broadcast receiver and receive that intent.
Update: here is the code I have promised.
At first you need to create broadcast receiver.
public class NotifyHandlerReceiver extends BroadcastReceiver {
public static final String ACTION = "me.pepyakin.defferednotify.action.NOTIFY";
public void onReceive(Context context, Intent intent) {
if (ACTION.equals(intent.getAction())) {
Notification.Builder builder = new Notification.Builder(context)
.setTicker("Notifica")
.setSmallIcon(android.R.drawable.stat_notify_chat)
.setContentTitle("Notifica")
.setContentText("Hai una notifica!")
.setAutoCancel(true)
.setContentIntent(PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0));
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify("interstitial_tag", 1, builder.build());
}
}
}
This is your broadcast receiver that can handle notification requests. For it can work, you must register it in your AndroidManifest.xml. If you don't do it, Android won't be able to handle your notification request.
Just add <receiver/> declaration into your <application/> tag.
<receiver android:name=".NotifyHandlerReceiver">
<intent-filter>
<action android:name="me.pepyakin.defferednotify.action.NOTIFY" />
</intent-filter>
</receiver>
Take a note, that action name be exactly as defined in NotifyHandlerReceiver.ACTION.
Then you can use this code
public static final int REQUEST_CODE_NOTIFY = 1;
public void scheduleNotification(long delayTimeMs) {
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
long currentTimeMs = SystemClock.elapsedRealtime();
PendingIntent pendingNotifyIntent = PendingIntent.getBroadcast(
this,
REQUEST_CODE_NOTIFY,
new Intent(NotifyHandlerReceiver.ACTION),
PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, currentTimeMs + delayTimeMs, pendingNotifyIntent);
}
from your activity to start a notification delayed on delayTimeMs amount of milliseconds.

Categories

Resources