Screen Off ,Broadcast Receiver does not call some time it will execute but mostly wifi state change event is called .i have also set up the priority of screen off but does not call or some time call .can you please tell .when my screen off i want to execute first then other wifi state changed will called
BroadcastReceiver wReceiver = new ScreenReciver();
#Override
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
filter.setPriority(1);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.setPriority(1000);
registerReceiver(wReceiver, filter);
}
#Override
protected void onPause() {
unregisterReceiver(wReceiver);
super.onPause();
}
public class ScreenReciver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
switch (wifiState) {
case WifiManager.WIFI_STATE_DISABLED:
Intent myintent = new Intent(context, TimerClockActivity.class);
myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
myintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myintent);
wifiStateText = "WIFI_STATE_DISABLED";
break;
default:
break;
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Intent myintent = new Intent(context, TimerClockActivity.class);
myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
myintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myintent);
}
}
}
You are calling unregisterReceiver(wReceiver); in onPause(). This means that every time that the Activity goes into the background (including when the screen turns off), your Activity is unregistered for that broadcast.
onPause() is likely getting called before your Activity gets a chance to receive the Broadcast.
Perhaps you want to place unregisterReceiver(wReceiver); in onDestroy() instead?
Related
I have an alarm application.
Flow looks like this :
WakefulBroadcastReceiver(Acquires wakelock) --->> Intent service -->> startActivity
public class AlarmService extends IntentService {
protected void onHandleIntent(Intent intent) {
Intent activityIntent = new Intent(this, TriggeredActivity.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(activityIntent);
Basically WakefulBroadcaseReceiver starts an intent service using startWakefulService(). Inside intent service's onHandleIntent(), only work I am doing is further starting a new activity using startActivity(). That new activity is where I am using mediaPlayer in a loop, which sounds the alarm. That activity has a dismiss button, which waits for user click to stop the media player & activity finishes.
Now the problem I am facing is that after calling startactivity() inside intent service, I can not wait for TriggeredActivity to finish(no equivalent to startActivityForResult in Service) and then complete wakeful intent. Related link
startActivity(activityIntent);
WakefulBCastReceiver.completeWakefulIntent(intent); /* can't do here */
So I am not explicitly releasing wakelock here.
My question is will the wakelock be released automatically(link-to-death), when the process that is holding it is killed.
If yes, then in my particular scenario, I need not call WakefulBCastReceiver.completeWakefulIntent().
Yes, you need to use completeWakefulIntent.
You need to put your TriggeredActivity intent into EXTRAs.
#Override
public void onReceive(Context context, Intent intent) {
Intent intentService = new Intent(context, NotificationsIntentService.class);
// Inserting data inside the Intent
intentService.putExtra(NotificationsIntentService.EXTRA_NOTIF, new Intent(context, TriggeredActivity.class));
startWakefulService(context, intentService);
}
NotificationsIntentService.class
public class NotificationsIntentService extends IntentService {
private static final String TAG = "DebugNotifIntent";
public static final String EXTRA_NOTIF = "extra_notif";
public NotificationsIntentService(){
super(NotificationsIntentService.class.getSimpleName());
}
#Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "onHandleIntent: ");
Intent extraIntent = intent.getParcelableExtra(EXTRA_NOTIF);
extraIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(extraIntent);
NotificationWakefulBroadcastReceiver.completeWakefulIntent(intent);
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
}
I have managed to find a solution for my problem. I am now using a Messenger for message based cross process communication between intent service & triggered activity.
I am passing a handler - alarmServiceHandler, from intent service to activity through a messenger.
Handler alarmServiceHandler = new Handler(){
#Override
public void handleMessage(Message msg) {
if(msg.arg1 == 1) {
completedTriggeredActivity = true;
}
}
};
Inside onHandleIntent(), I am passing handler through Messenger object in intent's extra data.
Messenger alarmServiceMessenger = new Messenger(alarmServiceHandler);
Intent activityIntent = new Intent(this, TriggeredActivity.class);
activityIntent.putExtra("AlarmServiceMessenger", alarmServiceMessenger);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(activityIntent);
while(!completedTriggeredActivity){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
WakefulBCastReceiver.completeWakefulIntent(intent);
In TriggeredActivity, I am retrieving messenger in Dismiss button's OnClickListener, just before calling finish() on the activity. And sending back a message to AlarmService with arg = 1, implying end of processing in triggered activity.
buttonDismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Messenger alarmServiceMessenger = getIntent().getParcelableExtra("AlarmServiceMessenger");
Message alarmServiceMessage = Message.obtain();
alarmServiceMessage.arg1 = 1;
try {
alarmServiceMessenger.send(alarmServiceMessage);
} catch (RemoteException e) {
e.printStackTrace();
}
finish();
}
After starting triggered activity, I am putting AlarmService in sleep mode till boolean variable completedTriggeredActivity has not been set to true in handleMessage(). Once true, it means triggered activity has finished & now I can proceed with releasing wake lock.
I would be glad to receive comments about my approach & any suggestions towards a better solution to my problem, than the one I have deviced.
How can I close my application when the Android goes to stand by either by user pressing the button or screen timeout?
I've tried onResume(), onPause and onRestart() methods but I already use then in another context.
Regards
In your application class onCreate() method register receiver for Screen Off Event
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.d(TAG, Intent.ACTION_SCREEN_OFF);
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.d(TAG, Intent.ACTION_SCREEN_ON);
}
}
}, intentFilter);
like this.
In my App, there is a condition which check every day and if it gets true then I want my App get close in between the run like a crash and stack also gets clear .
I have try and tested many solutions but didn't find the one that works the way i wanted .
My BroadcastReceiver:
public void onReceive(Context context, Intent intent) {
PreferenceForApp prefs = new PreferenceForApp(context);
Bundle bundle = intent.getExtras();
if (bundle!=null){
if(bundle.containsKey("exception")) {
// String e = bundle.getString("exception")
if(bundle.get("exception").toString().equalsIgnoreCase("http request failed with error_msg No Match Found")) {
prefs.setIsDeviceValidated(false);
prefs.setIsLogIn(false);
Log.i("Time", "Exception Occur");
Intent CSPIntent=new Intent(context,CSPLoginActivity.class);
CSPIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
CSPIntent.putExtra("close_activity", true);
Log.i("Time", "IntentExit");
context.startActivity(CSPIntent);
}
}
}
}
}
And code to finish in an Activity I am calling from broadcastReceiver:
if (getIntent().getBooleanExtra("close_activity",false)) {
Log.i("Time", "ExitCSPLogin");
this.finish();
}
This code is not closing App in between the run.
You need to register BroadcastReceiver in your activity and send broadcast to BroadcastReceiver when you want to close application.
In your Activity try this:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.package.ACTION_CLOSE");;
BroadcastReceiver Receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
registerReceiver(Receiver, intentFilter);
in onDestroy() method of you Activity unregister BroadcastReceiver:
#Override
protected void onDestroy() {
unregisterReceiver(Receiver);
super.onDestroy();
}
Now when you want close application send broadcast to BroadcastReceiver:
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.package.ACTION_CLOSE");
sendBroadcast(broadcastIntent);
Hope this helps!
you have to check below condition in your app's mainActivity's onCreate method every time when user enter in your app. or in onResume if you want to to close your app immediately
if (!prefs.getIsDeviceValidated()) {
Log.i("Time", "ExitCSPLogin");
this.finish();
}
i assume you have more then one activity in your app, so insted of check above flag in every activity we 'll put it in main activity. allow user to use your app until he/she come at mainActivity
Note: create Broadcast Receiver for your App(add in manifest), not for specific activity
public class myReceiver extends BroadcastReceiver {
public static boolean wasScreenOn = true;
#Override
public void onReceive(final Context context, Intent recievedIntent) {
if (recievedIntent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
wasScreenOn = false;
Intent intent = new Intent(context,
myActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
context.startActivity(intent
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
} else if (recievedIntent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
wasScreenOn = true;
}
}
}
The activity display after when action screen on is call.
myActivity.class
disableKeyguard using KeyguardManager and start service. In service register the Receiver. Now when user off the device then call ACTION_SCREEN_OFF and when tap home button then call ACTION_SCREEN_ON but the problem is occur when i press home buttom many times.
add one line in manifest in receiver tag
receiver android:name=".MyBoardCast" android:enabled="true"
I have a broadcast receiver which will launch an Activity onReceive.
When launched, this activity will perform a long task and call finish() at the end.
I do not want to trigger another launch of Activity when the previous Activity is still performing the long task. How can I launch only a singletask activity? I have set this in the manifest.
android:launchMode="singleTask"
In my onReceive method,
public void onReceive(Context context, Intent intent) {
Intent activity = new Intent(context, Preview.class);
activity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activity);
}
Don't seems to work.
The Activity class actually is a MediaRecorder which will record a video clip (say 10 sec). Thus I do not want to trigger another Activity while this recording is still incomplete.
You just need to have some way for your activity to communicate to your receiver to let it know whether or not it is already running. If you have that then you can make an if statement in the receiver that will keep it from launching multiples.
One option is a static boolean in your activity that indicates whether or not you are currently running. Then you can check that boolean from the receiver, and if it is true, then don't call startActivity().
your activity would need something like this:
public YourActivity extends Activity{
public static isRunning = false;
public void onStart(){
...
isRunning = true;
}
public void onStop(){
...
isRunning = false;
}
}
now in your receiver you can make a simple if statement that will check the value of isRunning:
public void onReceive(Context context, Intent intent) {
if(YourActivity.isRunning == false){
Intent activity = new Intent(context, Preview.class);
activity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activity);
}
}
Please add these flags.
public void onReceive(Context context, Intent intent) {
Intent activity = new Intent(context, Preview.class);
activity.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
context.startActivity(activity);
}