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.
Related
I would like to make an app that starts up every time the device is unlocked.
I'm new to Android and although I've read dozens of answers and the documentation there are just too many moving parts that I'm having a hard time troubleshooting.
Here is what I have ...
Structure
app
- manifests
-- AndroidManifest.xml
- java
-- DisplayMessageActivity.java
-- MainActivity.java
- res
- UnlockReceiver.java
This is in in my AndroidManifest.xml
<receiver android:name="UnlockReceiver">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
This is my UnlockReceiver.java class
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.xywebsolutions.myapplication.MainActivity;
public class UnlockReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
What am I doing wrong? Is it my Structure? Do I need to add permissions before adding the receiver?
You don't need any broadcast to start/stop video.
You can use onResume() of activity lifecycle to start video. onResume will be called when activity becomes visible to user.
onPause() of activity to pause the video. You can also save current play position and start from that position when activity started again using onSaveInstanceState() function
For your case you can try this
#Override
public void onReceive(Context context, Intent intent) {
//start activity
Intent i = new Intent();
i.setClassName("com.test", "com.test.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
two changes and this issue was fixed:
1) The "UnlockReceiver" was moved into the java folder
app
- manifests
-- AndroidManifest.xml
- java
-- DisplayMessageActivity.java
-- MainActivity.java
-- UnlockReceiver.java
- res
And
2) The receiver in the manifest needed android:exported="true" ...
<receiver android:name="UnlockReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
Hi I'm trying to catch the sms content and use in my app, so I made a BroadcastReceiver with Permission and Manifests But when the device receive sms, my code doesn't run which means that the BroadcastReceiver doesn't fire.
I also checked many Articles inside and outside here, there're some:
Android Sms Receiver Result to Main Activity
SMS receiver didn't work
Android SMS Receiver not working
Broadcast Receiver not working for SMS
Here is a part of my Manifest:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<application.
...
...
<receiver android:name="com.example.android.receiver.SmsReceiver"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
And This is my Receiver
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "SMS Received!", Toast.LENGTH_LONG).show();
}
}
I also tried to register the receiver dynamically inside activity onCreate() but nothing changed
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
intentFilter.setPriority(2147483647);
registerReceiver(new SmsReceiver(), intentFilter);
Does anyone know where is the problem? It should just Toast that a message is recieved so I can continue work but the receiver doesn't seem to even fire
You should read Automatic SMS Verification.
public abstract Task startSmsRetriever ()
Starts SmsRetriever, which waits for a matching SMS message until timeout (5 minutes). The matching SMS message will be sent via a Broadcast Intent with action
SmsRetriever.SMS_RETRIEVED_ACTION.
When you are ready to verify the user's phone number, get an instance of the SmsRetrieverClient object, call startSmsRetriever, and attach success and failure listeners to the SMS retrieval task:
SmsRetrieverClient mClient = SmsRetriever.getClient(this);
Task<Void> mTask = mClient.startSmsRetriever();
mTask.addOnSuccessListener(new OnSuccessListener<Void>() {
#Override public void onSuccess(Void aVoid) {
Toast.makeText(YourActivity.this, "SMS Retriever starts", Toast.LENGTH_LONG).show();
}
});
mTask.addOnFailureListener(new OnFailureListener() {
#Override public void onFailure(#NonNull Exception e) {
Toast.makeText(YourActivity.this, "Error", Toast.LENGTH_LONG).show();
}
});
When a verification message is received on the user's device, Play services explicitly broadcasts to your app a SmsRetriever.SMS_RETRIEVED_ACTION Intent, which contains the text of the message. Use a BroadcastReceiver to receive this verification message.
public void onReceive(Context context, Intent intent) {
if (SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
Bundle extras = intent.getExtras();
Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
switch(status.getStatusCode()) {
case CommonStatusCodes.SUCCESS:
// Get SMS message contents
String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
// Extract one-time code from the message and complete verification
// by sending the code back to your server.
break;
case CommonStatusCodes.TIMEOUT:
// Waiting for SMS timed out (5 minutes)
// Handle the error ...
break;
}
}
}
Register Your BroadcastReceiver with the intent filter com.google.android.gms.auth.api.phone.SMS_RETRIEVED in your app's AndroidManifest.xml file.
<receiver android:name=".YourBroadcastReceiver" android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED"/>
</intent-filter>
</receiver>
Finally, Register your BroadcastReceiver in your onCreate() section.
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(SmsRetriever.SMS_RETRIEVED_ACTION);
getApplicationContext().registerReceiver(broadcastReceiverOBJ, intentFilter);
For Demo purpose you should read Automatic SMS Verification Android
I found it myself. Here is code that works! It must contain SMS_DELIVER_ACTION which this does. (many on github do not!)
Go into Settings->AppsNotifications->DefaultApps->MessagingApp and "SMS App" will showup for you to pick.
https://android-developers.googleblog.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html
https://www.androidauthority.com/how-to-create-an-sms-app-part-2-724264/
https://github.com/treehousefrog/SMS-Project-Part-2
You should request runtime permission to receive SMS(Android 6.0 and higher).
https://developer.android.com/guide/topics/permissions/overview
First make another app your default SMS app.
Then: Google Hangout --> Settings(Disable merged conversations) --> SMS (Disable SMS)
Or,
This example for mainfest:
<receiver android:name=".SmsBroadcastReceiver"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="android.provider.Telephony.SMS_DELIVER" />
<action android:name="android.provider.Telephony.SMS_DELIVER_ACTION" />
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
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.
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.
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