BroadcastReceiver - how to trigger service every time user ON/OFF screen - java

Hello
I want to check whether my service is running or not, if service is running, do nothing.
But if service is not running, restart the service.
So I do something like this.
In Manifest.xml
<receiver android:name="com.varma.android.aws.receiver.Receiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Receiver.java
public class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("aws", "Received...");
if(isMyServiceRunning(context)) {
Log.v("aws", "Yeah, it's running, no need to restart service");
}
else {
Log.v("aws", "Not running, restarting service");
Intent intent1 = new Intent(context, Service.class);
context.startService(intent1);
}
}
private boolean isMyServiceRunning(Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (Service.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
But nothing is happening when I ON/OFF screen
What am I doing wrong?

You can't register screen on/off broadcast through manifest file,tt doesn't work(need to explore why).
Register it in your main activity through code
ifilter=new IntentFilter();
ifilter.addAction(Intent.ACTION_SCREEN_OFF);
ifilter.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(new Receiver(), ifilter);
Till the time your activity remains in memory you will receive these broadcasts in your receiver, i have tested it and able to receive broadcasts.
However if your activity gets finished you won't receive these broadcasts. So registerting these broadcasts in some LocalService in your app with START_STICKY feature will solve your issue.

If you want your service to stay up, you don't need to do this. Just have onStartCommand return START_STICKY. This will cause Android to restart any service it stops as soon as it has sufficient memory.

Related

How to get time change broadcast even if app closed

i have an app and it's important for me to detect time changes. so far i have receiver with proper intent filters like this:
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.ACTION_TIME_CHANGED" />
<action android:name="android.intent.action.DATE_CHANGED" />
</intent-filter>
</receiver>
and this is my receiver :
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
SharedPreferences.Editor spe =PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()).edit();
spe.putLong("UFCount", 1));
spe.putLong("FCount", 1));
spe.commit();
Toast.makeText(context.getApplicationContext(), "Your job is being done!!", Toast.LENGTH_LONG).show();
}
}
this receiver only works when the app is open\in-the-background . how can i make this receiver to work even if the app is closed?
Since the application is closed, there is no UI (i.e. no Activity) to display the Toast. At this point, there should be an exception in Logcat. You might also add a logcat trace before displaying the Toast, like Log.d("MyReceiver", "in onCreate()"); and you should see it even when the app is closed.
Instead of displaying a Toast, you could for instance send a local notification, which is allowed because it does not require an Activity from your app.

How can I keep my app's notification displayed when my app is updated on Google Play store?

My app has a function of set-notification. Here is my code.
#SuppressWarnings("deprecation")
public static void setNotification(Context _context) {
NotificationManager notificationManager =
(NotificationManager) _context.getSystemService(Context.NOTIFICATION_SERVICE);
int icon_id = R.drawable.icon;
Notification notification = new Notification(icon_id,
_context.getString(R.string.app_name), System.currentTimeMillis());
//
Intent intent = new Intent(_context, MainActivity.class);
notification.flags = Notification.FLAG_ONGOING_EVENT;
PendingIntent contextIntent = PendingIntent.getActivity(_context, 0, intent, 0);
notification.setLatestEventInfo(_context,
_context.getString(R.string.app_name),
_context.getString(R.string.notify_summary), contextIntent);
notificationManager.notify(R.string.app_name, notification);
}
This code works fine. If my app is closed, notification keeps displayed.
But even if notification was set, the notification is cancelled when user will update my app version by Google Play store.
I know that...
The notification is cancelled when my app is uninstalled.
In fact an update is "uninstall and install".
How can I keep displayed when my app version is updated?
If I understood right you want displaying a notification after updating.
So you can implement receiver that listens updating of app or rebooting of device and shows notification again.
add to you manifest:
<receiver
android:name=".UpdatingReceiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
and implement receiver:
public class UpdatingReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()) || Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())) {
// check is need to show notification
}
}

Broadcast is not getting received[LocalBroadcastManager] Android

Can you help me resolve this issue, I have implemented a receiver(registered via XML) which listens for particular local broadcasts, and in turn starts a service for further processing, but somehow that receiver is not receiving any broadcasts.
Although another receiver, which is registered locally through code is able to receive the broadcast, can you help me fix this. Below is my code.
// Sending broadcast
Intent intent = new Intent(Constants.ACTION_PROFILE_UPDATED);
LocalBroadcastManager.getInstance(POC.getAppContext()).sendBroadcast(intent);
// Receiver
public class LocalReceiver extends BroadcastReceiver {
private final String TAG = LocalReceiver.class.getSimpleName();
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "received"); // its not received
if(intent.getAction() != null){
String action = intent.getAction();
Log.i(TAG, "action = " + action);
if(action.equals(Constants.ACTION_PROFILE_UPDATED)){
// IN manifest
<receiver
android:name=".LocalReceiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="local.action.profile.updated" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
This damn code is not working, no where in developer guide it says local broadcast wont be received, through receiver registered through xml.
Please help,
Thanks.
I have implemented a receiver(registered via XML) which listens for particular local broadcasts
That is not possible. LocalBroadcastManager does not work with manifest-registered receivers, only with receivers registered via registerReceiver(), called on the LocalBroadcastManager instance itself.

When you plug in charger what service and classes android calls?

I have been trying to use reflection in case when anybody plug in charger or usb cable to charge their device. Can anybody please tell me what does android implements to interact with the hardware.
I am not exactly interested in the USB
I am interested more in the red(amber in case of HTC) led that glow when we connect our device for charging or in case of notification.
Set up a BroadcastReceiver for ACTION_BATTERY_CHANGED. An Intent extra will tell you what the charging state is -- see BatteryManager for details.
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".receiver.PlugInControlReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
</application>
Then
public void onReceive(Context context , Intent intent) {
String action = intent.getAction();
if(action.equals(Intent.ACTION_POWER_CONNECTED)) {
// Do something when power connected
}
else if(action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
// Do something when power disconnected
}
}
see this link
you can monitor battery charging state by registering following intent filter
< receiver android:name=".PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
and in code
public class PowerConnectionReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
}
}

Broadcast Receiver for ACTION_USER_PRESENT,ACTION_SCREEN_ON,ACTION_BOOT_COMPLETED

I am creating a class which uses broadcast receiver. I want to receive the broadcast on unlocking of the phone. But there is some issue. Please help me out.
My Manifest.xml is :-
<receiver android:name=".MyReciever">
<intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_USER_PRESENT" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_SCREEN_ON" />
</intent-filter>
</intent-filter>
</receiver>
and my Broadcast reciever class :-
public class MyReiever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("My Reciever","is intent null => " + (intent == null));
Log.d("My Reciever",intent.getAction()+"");
}
}
Though other application and services are receiving broadcast for "Screen_on" and "USer_Present" eg. WifiService.
Although the Java constants are android.content.intent.ACTION_USER_PRESENT, android.content.intent.ACTION_BOOT_COMPLETED, and android.content.intent.ACTION_SCREEN_ON, the values of those constants are android.intent.action.USER_PRESENT, android.intent.action.BOOT_COMPLETED, and android.intent.action.SCREEN_ON. It is those values which need to appear in your manifest.
Note, however, that a receiver for ACTION_SCREEN_ON can not be declared in a manifest but must be registered by Java code, see for example this question.
Since implicit broadcast receivers are not working as of Android 8.0, you must register your receiver by code and also in the manifest.
Do these steps:
Add manifest tag
<receiver android:name=".MyReciever">
<intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_USER_PRESENT" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_SCREEN_ON" />
</intent-filter>
</intent-filter>
</receiver>
Create a receiver class and add your codes
public class MyReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("My Reciever","is intent null => " + (intent == null));
Log.d("My Reciever",intent.getAction()+"");
}
}
Create a service and register the receiver in it
public class MyService extends Service {
MyReceiver receiver = new MyReceiver();
#Override
public IBinder onBind(Intent intent) { return null; }
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
registerReceiver(receiver);
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}
Don't forget to define the service in manifest
<service android:name=".MyService"/>
To make your broadcast work, you have to register it in service. And to keep your service alive you can use tools like alarm managers, jobs, etc which is not related to this question.
Check your Class name, that is extending BroadcastReceiver. It should be "MyReciever" not "MyReiever"
Beside Typo that mentioned in earlier answers and the fact that the receiver class package name should be completely mentioned in receiver tag, I think the problem is that the code in question uses two nested intent filters. I believe this code will work correctly:
<receiver android:name=".MyReciever">
<intent-filter>
<action android:name="android.intent.action.ACTION_USER_PRESENT" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_SCREEN_ON" />
</intent-filter>
</receiver>
I can only give you a quick tip as i have gone through that path you are following with much less success. Try to read logcat using java.util.logging so that you will not require permission to read logs. And in log view create listener for the one containing "system disable" as its header. it fires up both at lock and unlock. check for the one that gives access to system.android not the other screen.
Hope it helps. Best of luck

Categories

Resources