This is my requirement.
I have an application which can be force kill by user. I want this application to receive broadcast from other app to execute some task even it is forced kill.
I am trying to make another app with service which will send a broadcast in event 1 mins to my first application.
My first application should receive this broadcast even it is forced kill.
This is what I am trying to do.
in First app:
BroadcastReceiver dummy = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("broadcast Received","broadcast Received");
}
};
IntentFilter filter = new IntentFilter("com.action.blockapp");
registerReceiver(dummy,filter);
In my second app.
Intent intent = new Intent("com.action.blockapp");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setPackage("com.example.myapplication2");
sendBroadcast(intent);
I am not able to receive the broadcast when my app is forced kill.
Please suggest.
My point . You need to declare revice in the service running in the background. Your specific revice declared in the manifest<receiver android:name=".service.NotifyReceiver" />
</application>
When the service is run. You can get notifications from revice like below
Intent intent1 = new Intent(context, NotifyReceiver.class);
intent1.setAction(action);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
Hope to help you!
You have registered your BroadcastReceiver dynamically, in code. When your app is force stopped, that code is no longer running and your BroadcastReceiver no longer exists. It is no longer registered.
You will need to create a proper class that extends BroadcastReceiver and create a manuifest entry <receiver> for that with an <intent-filter> that matches the broadcast Intent you are broadcasting.
Related
I have am alarm manager through which i set a time to trigger on it by providing a broadcast receiver, onReceive method of broadcast receiver calls successfully but any idea how to launch app from onReceive if app is not running in foreground?
Menifest
<receiver
android:exported="true"
android:name=".AlarmReceiver" />
BroadcastReceiver
#Override
public void onReceive(Context context, Intent intent)
{
Intent scheduledIntent = new Intent(context, MainActivity.class);
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(scheduledIntent);
}
MainActivity
AlarmManager alarmManager=(AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent=new Intent(this,AlarmReceiver.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(this,1000,intent,0);
alarmManager.set(AlarmManager.RTC_WAKEUP,timeInMillis,pendingIntent);
Kindly help
Thanks
It is basically a radio music stream using exoplayer , what we need is user set a time on which he want it to play , and when that time comes stream start playing automatically .
A background service will only run for one minute on Android 8.0+. For a music player, you will want a foreground service, where the associated Notification gives the user playback controls (e.g., pause, resume, stop).
First, you should use workManager
Also, Brodcastreciver has limits on different android versions doc
I have an app hooked with push notifications. When the user click on the notification, I want a broadcast receiver to be triggered instead of an activity.
I looked at these threads and looks like this is perfectly possible and common.
Android Notification Action is not fired (PendingIntent)
Send broadcast on notification click
However I can't get it to work. I see the notification, but when I click on it and it never triggers my Broadcast receiver.
Here what I have tried:
I created a custom receiver:
public class NotificationOnClickReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
Log.d(Constants.Engine.LOGGER_TAG_GCM,
NotificationOnClickReceiver.class.toString() + " triggered");
}
}
I registered this receiver dynamically on my global Application (not activity)
mNotificationOnClickReceiver = new NotificationOnClickReceiver();
IntentFilter intentFilterOnClick = new IntentFilter(Constants.Engine.BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK);
getApplicationContext().registerReceiver(mNotificationOnClickReceiver, intentFilterOnClick);
Log.e(Constants.Engine.LOGGER_TAG_DBG, "mNotificationOnClickReceiver = " + mNotificationOnClickReceiver.toString());
The filter is just a string constant (I wonder if this is the problem)
public static final String BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK = "push_notification.onclick";
Receiver intent setup:
// When the user taps on the notification bar, this is the receiver that I want to be called
Intent pushNotificationIntent;
Log.e(Constants.Engine.LOGGER_TAG_DBG, "Notification called!");
pushNotificationIntent = new Intent(this, NotificationOnClickReceiver.class);
// Not sure if this causing the problem
pushNotificationIntent.setAction(Constants.Engine.BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK);
// ensures that each notification and intent are unique
int uniqueCode = new Random().nextInt(Integer.MAX_VALUE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context,
uniqueCode,
pushNotificationIntent, // Receiver Intent
PendingIntent.FLAG_CANCEL_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(mIconId)
.setContentTitle(context.getString(R.string.myString))
.setContentText("My GCM Alert!")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
From the logs, things seem to work except that my Broadcast receiver never gets called:
02-22 15:47:47.092 16863-16863/com.myapp.test E/MYAPP_DBG: mNotificationOnClickReceiver = mytest.broadcastreceivers.NotificationOnClickReceiver#d78f559
02-22 15:47:53.312 16863-17038/com.myapp.test E/MYAPP_DBG: Notification called!
If anyone has this working, can you point me out a sample code? I don't see where my bug is.
thank you!
This would appear to be the problem:
pushNotificationIntent = new Intent(this, NotificationOnClickReceiver.class);
You're actually creating an explicit Intent for the broadcast PendingIntent. Explicit Intents do not work with dynamically registered Receivers, as they target a Receiver class, rather than an instance.
You'll need to use an implicit Intent instead, which you can do by simply instantiating the Intent with the action String, rather than a Context and Class. For example:
pushNotificationIntent = new Intent(Constants.Engine.BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK);
Please note that if your app's process is killed before the Notification is clicked, that dynamically registered Receiver instance will die, too, and the Notification's broadcast won't really do anything.
Depending on the desired behavior, it might be preferable to instead statically register your Receiver class in the manifest, and stay with the explicit Intent. Doing this will allow your app to receive the Notification broadcast even if its process has been killed.
To do that, you'd just need to add a <receiver> element for your NotificationOnClickReceiver in the manifest.
<receiver android:name="NotificationOnClickReceiver" />
Though you can still set an action on that broadcast Intent, you don't need an <intent-filter> for it here, as the explicit Intent is directly targeting the class anyway. If the Receiver is only for that one function, you don't necessarily need the action, and can just remove that altogether.
You can then remove the code you have in step 2, as you'd no longer need the dynamically registered instance.
To send a broadcast use this sendBroadcast(new Intent("NotificationClicked"));
Now to recieve the broadcast, mention this in your manifestunder application tag
<receiver android:name=".NotificationOnClickReceiver" >
<intent-filter>
<action android:name="NotificationClicked" >
</action>
</intent-filter>
</receiver>
I am bit new to android. I would like to know how to communicate with a foreground started service.
So, I got a Foreground service with a notification.
This notification has a (X) button to stop the service.
The service got a Static broadcastreceiver.
public static class NotificationStopButtonHandler extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Close Clicked",Toast.LENGTH_SHORT).show();
Log.i(LOG_TAG, "In Closed");
// imposible to do context.stopForground(true) or
// to call any other private coded by me
}
}
So my question is :
Is BroadcastReceiver is the best way ?
If it is : How I can communicate with the service to call stopForeground in the broadcastReceiver ?
Thanks in advance for your responses.
Same question like mien... But I would like to know which are the other solution than broadcastReceiver. thx
In your notification you will have a PendingIntent for the X button. I presume you have built that PendingIntent with
PendingIntent.getBroadcast(/* ... */);
What you can do instead is to create a PendingIntent for your service
Intent intent = /* intent for starting your service */;
intent.putExtra("STOP_FOREGROUND", true);
PendingIntent.getService(context, requestCode, intent, flags);
and in the intent you pass to the PendingIntent you would add an extra (STOP_FOREGROUND). When this intent is fired, your service will get called in onStartCommand(). Here you check the intent and if it contains your extra, you know you're expected to call stopForeground.
Instead of broadcasts, you can use PendingIntent with an Intent to the Service and tell the Service to shut down. You assign the PendingIntent to the close button action and/or to the notifications onDelete call when you build the notification.
Assuming that you're starting the Service with the notification, you can put commands in the Intent to tell the service to stop itself. Service#onStartCommand will be called on the service with the new Intent. The service checks for the shutdown call and calls stopSelf() when done.
Basically, the reason this works is because there can only be one Service started. Every subsequent attempt to start the service will send the intent to Service#onStartCommand, but it will not restart the Service. Thus, this is a way you can send commands to the service through means outside of binding. Plus it's way cleaner than using broadcasts.
I need to send broadcast from my one application to another applicaion.. any help!
my application package are 1)com.demo.database and 2)com.demo.list
Intent themesIntent = new Intent(ThemesManager.THEMES_UPDATED);
themesIntent.putExtra("package", packageName);
ctx.sendBroadcast(themesIntent);
not working..
Edits :
<receiver android:name="com.sample.ThemesUpdatedReceiver">
<intent-filter>
<action android:name="com.sample.THEMES_UPDATED"/>
</intent-filter>
</receiver>
#Ajit: Hi, Since Android API 3.0 [API level 11], If an application has never been started even once, then it's BroadcastReceiver can't receive events.As, in your case, your app has no launcher activity, so may be it is the case that causes rejection of event.
Along with that please try using below approach:
You have passed that constant value while creating Intent object. Instead pass it in method intent.setAction();
Hope this helps.
I figured that every sent broadcast is received by all applications except when you setPackage to the sending intent for specific package broadcast.
I am not receiving broadcast because my another app is not launched(that doesn't have launcher activity).
If you're going to broadcast, it generally follows you have a sender and receiver. You've posted what looks like the sender ..
sender (where ever you're sending from):
Intent toret = new Intent();
toret.setAction("com.myapp.foo");
toret.putExtra("bar", "fizzbuzz");
sendBroadcast(toret);
receiver (in eg onResume())
IntentFilter intentFilter = new IntentFilter("com.myapp.foo");
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// ... do something with the intent
}
// register the receiver
this.registerReceiver(receiver , intentFilter);
Sender always sends, receiver needs to register to listen for the intent.
I have a Service that uses a custom Connection class (extends thread) to a hardware controller. When the User prefers, I wish to maintain this connection on a permanent basis. I already have the code to handle when the Android device loses its internet connection, switches between wi-fi, etc.
In order to stay connected, the controller requires that you speak to it within every 5 minutes. I currently, within the Connection class start a thread that runs in a while(), and checks the system time and the last time it communicated, and when > 4 minutes it requests a status. For some reason, at different times the communication doesn't occur in time. i.e., occurs after 5 minutes. The Service doesn't die, as far as I can tell but the "Ping" to the controller is late. This doesn't happen when I have the phone plugged into the charger (or debugger). Additionally, the behavior is the same when I move the Service to the foreground.
Does the phone slow down it's processor when it goes to sleep?
Is there a better way?
I'm thinking it's the AlarmManger, but I'm having trouble getting it to work with an inner-class, within the Service. I tried using the API demos as a starting point, but I can't seem to figure out how to get the Broadcast receiver registered. I am trying to register the receiver programmatically, with no changes to the manifest.
public class DeviceConnectionService extends Service {
#Override
public void onCreate() {
Intent intent = new Intent(this, PingConnection.class);
intent.setAction("KEEP_CONNECTION_ALIVE");
PendingIntent sender = PendingIntent.getBroadcast(this,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// We want the alarm to go off 30 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
firstTime += 15*1000;
// Schedule the alarm!
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstTime, 15*1000, sender);
// register to listen to the Alarm Manager
if (mPingConnectionReceiver == null) {
mPingConnectionReceiver = new PingConnection();
getApplicationContext().registerReceiver(mPingConnectionReceiver,
new IntentFilter("KEEP_CONNECTION_ALIVE"));
}
}
// ...
public class PingConnection extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (dBug) Log.i("PingConnection", "Pinging Controller");
// do real work here
}
}
}
Does the phone slow down it's processor when it goes to sleep?
The phone shuts down its processor when it goes to sleep. That is the definition of "sleep".
I'm thinking it's the AlarmManger, but I'm having trouble getting it to work with an inner-class, within the Service. I tried using the API demos as a starting point, but I can't seem to figure out how to get the Broadcast receiver registered. I am trying to register the receiver programatically, with no changes to the manifest.
That is an unusual approach for AlarmManager. That being said, since you declined to describe "having trouble" in any detail, it is difficult to help you.
Get rid of getApplicationContext() (you don't need it and really don't want it in this case). I would register the receiver before touching AlarmManager. Before you go to production, please choose an action name that has your package name in it (e.g., com.something.myapp.KEEP_CONNECTION_ALIVE).
Beyond that, check LogCat for warnings.
UPDATE
In your LogCat, you should have a warning from AlarmManager complaining about not being able to talk to your BroadcastReceiver.
Replace:
Intent intent = new Intent(this, PingConnection.class);
intent.setAction("KEEP_CONNECTION_ALIVE");
with:
Intent intent = new Intent("KEEP_CONNECTION_ALIVE");
and you may have better luck.
you can't register AlarmManager in a Service.
All you can do is declare it as global in the Manifest.xml.
You can start the alarm from service in this way, by declaring it in Manifest.xml
If you have a remote service and you close the launcher activity, the AlarmManager will still run, but don't forget to stop it on onDestroy() method of the service.
I've tried to register only in the Service the AlarmManager as I didn't used it for the main activity, but no success!
It didn't work as registering as a normal BroadCastReceiver.
that's how the things are, you have to declare it in Manifest.xml as global
I know it's late, but maybe it's useful for someone else.
You can register it, the problem is when the Intent tries to call it.
Instead of calling it like this:
Intent intent = new Intent(this, PingConnection.class);
Create an empty intent and add an action you are going to listen to:
Intent intent = new Intent();
intent.setAction("value you want to register");
Then create the pending intent and send the broadcast like you have it.
Create an attribute for the receiver so you can access it in the whole class and unregister if necessary (if the pendingIntent is also an attribute you can unregister any time):
private PingConnection pingConnection = new PingConnection();
Register it like this:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("the value you used before");
getApplicationContext().registerReceiver(pingConnection, filter);
Now you won't get any errors, and the class is not static, and it's an inner class.