BroadCast Receiver calling intent after some time android - android

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"

Related

screen off does not Recive Broadcast

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?

Multiple custom broadcasting (how to handle)

I want to add a custom broadcast receiver to my app. and I have 3 methods that should do the broadcast. and in my onReceive method i want to identify which method did the broadcast. I have 3 methods like this
public void method01(View v){
int flag = 1;
Intent intent = new Intent();
intent.addFlags(flag);
broadcastIntent(intent);
}
public void method02(){
int flag = 2;
Intent intent = new Intent();
intent.addFlags(flag);
broadcastIntent(intent);
}
public void method03(){
int flag = 3;
Intent intent = new Intent();
intent.addFlags(flag);
broadcastIntent(intent);
}
and this is my broadcastIntent method
public void broadcastIntent(Intent intent){
sendBroadcast(intent);
}
in my onReceive method i use getFlags() method to get the flag value from the intent and send it through a if, else. but this do not work. any suggestion for improvements are welcome.
You can also use Actions to identify each one of you Intent objects.
String action1 = "first_sender";
String action2 = "second_sender";
Intent createIntent01(){
Intent intent01 = new Intent();
intent01.setAction(action1);
return intent01;
}
Intent createIntent02(){
Intent intent02 = new Intent();
intent01.setAction(action2);
return intent02;
}
And in your onReceive method you can use the getAction() method of intents to check which action was sent. This is in case you're not already using Actions.
[[EDIT]]
To register a BroadcastReceiver you need to define an IntentFilter and register the actions you wish to receive this way:
mBroadcastReceiver broadcastReceiver = new mBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(action1);
intentFilter.addAction(action2);
registerReceiver(broadcastReceiver,intentFilter);
class mBroadcastReceiver extends BroadcastReceiver{
public void onReceive(Context arg0, Intent arg1){
String action = arg1.getAction();
if(action.equals(action1)){
//do something
}else if(action.equals(action2)){
//do something else
}
}
The first problem is that ypu didn't specify a target for your intent. You can use intent filters and actions like rodkarom suggested or specify receiver's class directly (see in my example). In both cases you need either to declare your broadcast receiver in AndroidManifest.xml, or register it at runtime (see rodkarom's answer for a sample).
The method addFlags is used to specify some internal properties of Intent (like start activity corresponding to this intent in a new task) , so you cannot use it for your own data. The list of possible flags is in the documentation for setFlags method.
You can use putExtra to achieve your goal:
// an Activity is just an example
public class SenderActivity extends Activity {
// ...
void method01() {
int flag = 1;
Intent intent = new Intent(getApplicationContext(), Receiver.class); // any Context is acceptable here
intent.putExtra(MyReceiver.EXTRA_FLAG, flag); // any string will do well, you just need it to be the same here and in getExtra later
sendBroadcast(intent);
}
}
public class MyReceiver extends BroadcastReceiver {
public static final String EXTRA_FLAG = "your.package.name.EXTRA_FLAG";
// and in onReceive
public void onReceive (Context context, Intent intent) {
int flag = intent.getIntExtra(EXTRA_FLAG, someDefaultValue);
if (flag == 1) {
// ...
}
// ...
}
}
I found out the way to this and thought of sharing the code. this is the broadcasting done in my main activity for 2 different methods.
public void method1(View view){
Intent intent = new Intent();
intent.setAction("method1");
sendBroadcast(intent);
}
public void method2(View view){
Intent intent = new Intent();
intent.setAction("method2");
sendBroadcast(intent);
}
and this is how i receive it..
public class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Broadcast Intent Detected.",Toast.LENGTH_LONG).show();
}
this is how i registered it on manifest.
<receiver android:name="Receiver" >
<intent-filter>
<action android:name="method1" >
</action>
<action android:name="method2" >
</action>
</intent-filter>
</receiver>
Hope this will help if any one else came up with similar problem. and big thank you to every one who posted their answers here.

lock screen sometime not display android

i created lock screen application.. Sometimes it give me a problem. problem is that some time default lock screen display rather than my lock screen and some time directly home screen display. i donot know where is the problem. please give me suggestion about this.
LockScreenActivity:
Use
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
and create ScreenReceiver:
ACTION_SCREEN_OFF
ACTION_SCREEN_ON
ACTION_BOOT_COMPLETED
Refer this link: http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
in manifeast file : declare service and receiver and permission
The problem is in BroadcastReceiver when
if (recievedIntent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//call activity
}else if (recievedIntent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
}
but the activity call after screen on.. so what is missing or what is problem.. why it is happn pls rply.
Hi here is my code :
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 is calling after when action screen on is call .

Android - Broadcast Receiver launch a singletask Activity

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);
}

BroadcastReceiver how to start new intent

I implemented a broadcast receiver to "block" my app if the internet connection is lost.
By block I mean that the app has to open a "No internet connection" activity.
this is my receiver code:
public class ConnectivityReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
Log.d("** Debug **","noConnectivity " + noConnectivity);
if(noConnectivity){
//SHOW NO INTERNET CONNECTION ACTIVITY
}
}
}
Is it possibile to start NoInternetConnection.class when noConnectivity == true??
Thanks!
SOLUTION:
Intent i = new Intent(context, NoInternetConnection.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
You should just have to call startActivity:
context.startActivity(new Intent(NoInternetConnection.class));
You will need to make sure the "NoInternetConnection" activity is registered in your manifest file:
<activity android:name=".NoInternetConnection"></activity>
What issues are you having specifically?

Categories

Resources