Log Android user wakeup/unlock - android

I'm trying to create an application that has a constant running service which will log and display to the user everytime they unlock or change the phones state from screen off to screen on.
Is this possible without using the Log cat through the P.C?

Yes you can do it using Broadcast Receiver. Android system sends always broadcasts for specified changes. You can handle this broadcasts using Broadcast Receiver.
Example:
We will create a new Broadcast Receiver to handle the Screen On and Screen Off state:
public class PhoneStateBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//we will handle here the activities which will tell us that
// the screen state has changed
if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
//write log that the screen is off
}else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
//write log that the screen is on
}
}
}
All what you need is to declare this BroadcastReceiver in Android Manifest.xml
file:
<receiver android:name="yourpackage.PhoneStateBroadcastReceiver">
<intent-filter>
</intent-filter>
</receiver>
And after that you should start your PhoneStateBroadcastReceiver on yout
Service onCreate Method:
//...
public void onCreate(){
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
PhoneStateBroadcastReceiver pSReciever = new PhoneStateBroadcastReceiver();
registerReceiver(pSReciever, filter);
}

Related

Android make the application autorun after unlocking the screen even if the application is not running

I'm trying to create an autorun service: so that the application launches every time after unlocking the screen, after entering the graphic key or password if it exists (on Android 7,8,9,10). I wrote the code dynamically through the borocast receiver (ACTION_SCREEN_OFF) but it works while the application is on the stack (running) and I want it to always start. The method through registering in the manifest in android 9 already does not work the listeners. How to implement this?
public class WordsBase extends AppCompatActivity {
ScreenReceiver resiverStart;
#Override
protected void onPause() {
super.onPause();
resiverStart= new ScreenReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(resiverStart,filter);
}
}
public class ScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Intent intent1 = new Intent(context, WordsBase.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
throw new UnsupportedOperationException("Not yet implemented");
}
}
I understand that you want to do the following:
If the user unlocks the device, you want to start your app.
Why don't you do the following:
Use the USER_PRESENT receiver (android.intent.action.USER_PRESENT). Please not that you have to register explicitly to this receiver, just registering it in the manifest is not enough
If the respective broadcast is fired, start your app and make sure you are still registered to the broadcast (to have your app started again the next time the user unlocks the device).

Broadcast listener to user touch in Android

I want to know the time left before screen off.
I can easily get the timeout defined by system, but the counter resets with each touch the user do. so I need to reset the counter on my side as well.
I don't want to have an activity in foreground but to use (probably) a broadcastReceiver.
You can't do this but you can detect when screen off and screen on - Use actions:
Intent.ACTION_SCREEN_ON
Intent.ACTION_SCREEN_OFF
But do not put it in AndroidManifest.xml you must register this broadcast, example:
public class ScreenReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent){
String action = intent.getAction();
if(action.equals(Intent.ACTION_SCREEN_ON)){
// screen on
} else {
// screen off
}
}
}
To register broadcast (do not call it in this broadcast (ScreenReceiver) - call it in Service, Activity or Application):
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(new ScreenReceiver(), filter);
I guess the best solution is register broadcast in service because when Activity calls onDestroy() broadcast may be automatically unregistered.

BroadcastReceiver for screen lock not being triggered

I'm trying to turn the GPS location updates off when the screen locks. Having read the answer to this question Android - how to receive broadcast intents ACTION_SCREEN_ON/OFF?
I've written some code to implement a BroadcastReceiver but it's not working when the screen goes off.
I've registered a BroadcastReceiver in my code with
#Override
public void onResume() {
super.onResume();
IntentFilter iFilter = new IntentFilter();
iFilter.addAction("android.intent.action.ACTION_SCREEN_ON");
iFilter.addAction("android.intent.action.ACTION_SCREEN_OFF");
registerReceiver(screenStatReceiver, iFilter);
and the receiver itself is just a stub for now:
public BroadcastReceiver screenStatReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d("BCAST_TAG", "Got broadcast");
String action = intent.getAction();
}
};
and in the manifest I have:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Any ideas as to why it's not being triggered when I debug it on my phone?
For this problem, you have to create a infinite service, which is registering a local broadcast receiver for these intents. If you do this way, then your app will look for screen off but make sure you have to make service which will always running in background like reboot receiver
You are registering the reciever in the onResume() method. Why dont you register it in the onCreate() so you can have persistent "listening"?
In this case you are listening for changes only after the activity is resumed.

Update the UI of the calling activity or start a new activity when the alarm is triggered from a broadcast receiver

I am writing an alarm code and using a broadcast receiver. I am able to receive the broadcast receiver. but now I want to come back to the calling activity and update the UI of my activity. I am not able to this.
I used the following code in my activity but it is never executing that code.
private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "I am back!!", Toast.LENGTH_LONG).show();
}
};
#Override
protected void onPause()
{
super.onPause();
unregisterReceiver(myBroadcastReceiver);
}
#Override
protected void onResume()
{
super.onResume();
IntentFilter intentFilter = new IntentFilter("com.test.Main");
registerReceiver(myBroadcastReceiver, intentFilter);
}
in the manifest file I have included the following, here gotAlarm is the broadcast receiver file
<receiver android:name=".gotAlarm"
android:enabled="true">
</receiver>
gotAlarm file is one which gets called from the pending intent of the alarm set
public class gotAlarm extends BroadcastReceiver {
public void onReceive(Context context, Intent intent){
Toast.makeText(context, "Wake Up!!", Toast.LENGTH_LONG).show();
}
}
May be I am missing something very basic.
please help.
Two things:
If you dynamically register the receiver via Context.registerReceiver() then you won't receive broadcasts when Activity is paused (or stopped or not-running). If you need to receive broadcasts even when Activity is paused then create a top-level BroadcastReceiver class (as opposed to your inner class) and use <receiver> to register it.
BroadcastReceiver lifecycle docs state that BroadcastReceiver object is alive only during processing of onReceive(). You can not do any async tasks like showing dialogs, etc.. In your case (Activities might not be running and you receive a broadcast) you should use NotificationManager to notify user something happened.
I have dropped this way and I am starting a new activity on receiving broadcast. And I am sending information data from calling activity to broadcast and from broadcast to next activity. This has served the purpose.
Did you register your BroadcastReceiver (you can do this in the 'onResume'-method of your Activity)? Also, you should unregister your BroadcastReceiver in the 'onPause'-method.

Android-Broadcast Receiver and Intent Filter

I am new to android platform.please help me out how the Broadcast Receiver and Intent Filter behaves in android.please explain in simple line or with example.thanks in advance...
A broadcast receiver is a class in your Android project which is responsible to receive all intents, which are sent by other activities by using android.content.ContextWreapper.sendBroadcast(Intent intent)
In the manifest file of you receicving activity, you have to declare which is your broadcast receiver class, for example:
<receiver android:name="xyz.games.pacman.network.MessageListener">
<intent-filter>
<action android:name="xyz.games.pacman.controller.BROADCAST" />
</intent-filter>
</receiver>
As you can see, you also define the intent filter here, that is, which intents should be received by the broadcas receiver.
Then you have to define a class which extends BroadcastReceiver. This is the class you defined in the manifest file:
public class MessageListener extends BroadcastReceiver {
/* (non-Javadoc)
* #see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
*/
#Override
public void onReceive(Context context, Intent intent) {
...
}
Here, all intents which are passed through the filter are received and you can access them using the parameter passed in the method call.
A BroadcastReceiver can be registered in two ways: dynamic or static. Static is nothing but declaring the action through an intent-filter in AndroidManifest.xml to register a new BroadcastReceiver class. Dynamic is registering the receiver from within another class. An intent-filter determines which action should be received.
To create a BroadcastReceiver, you have to extend the BroadcastReceiver class and override onReceive(Context,Intent) method. Here you can check the incoming intent with Intent.getAction() and execute code accordingly.
As a new class, static would be
public class Reciever1 extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String str = intent.getAction();
if(str.equalsIgnoreCase("HELLO1")) {
Log.d("Abrar", "reciever....");
new Thread() {
public void run() {
Log.d("Abrar", "reciever....");
System.out.println("Abrar");
}
}.start();
}
or, if placed inside an existing class, it is called dynamically with
intentFilter = new IntentFilter();
intentFilter.addAction("HELLO1");
//---register the receiver---
registerReceiver(new Reciever1(), intentFilter);
BroadcastReceiver : 'Gateway' with which your app tells to Android OS that, your app is interested in receiving information.
Intent-Filter : Works with BroadcastReceiver and tells the 'What' information it is interested to receive in. For example, your app wants to receive information on Battery level.

Categories

Resources