In the onReceive(), I want to create a new Intent and start a new Activity. After the activity finishes, I want to get back to the same line from where I left for the activity. I know that I cannot use BroadcastReceiver class for achieving this. How can this be achieved? Below is the onReceive() code:
final BroadcastReceiver mPairingRequestRecevier = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(intent.getAction()))
{
final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
if (type == BluetoothDevice.PAIRING_VARIANT_PIN)
{
abortBroadcast();
String passkey="hey";
Intent passkeyIntent = new Intent(context.getApplicationContext(),TestActivity.class);
passkeyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
passkeyIntent.putExtra("passkey",passkey);
context.startActivity(passkeyIntent);
Log.d("after returning...",passkey);
}
else
{
Log.d("Unexpected pairingtype:" , type+"");
}
}
}
};
Any idea that can achieve this would be appreciated.
You can use startActivityFor result instead of startActivity to start an activity..make it perform some task and return the result to previous activity
Look at this: How to manage `startActivityForResult` on Android?
Related
I have 2 app, A and B, and i want send 2+2 from application A to B and in return i want to receive 4 from App B, please tell me the process and full code base.
1.From app A Trigger a broadCast1 with both of your numbers.
Intent intent = new Intent("com.myapps.appA");
intent.putExtra("num1",2);
intent.putExtra("num2",2);
sendBroadcast(intent);
now register the receiver for broadCast1 in App B, you can do this in onCreate of its Main activity.
private BroadcastReceiver broadcastReceiver1;
...
broadcastReceiver1 = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
}
};
registerReceiver(broadcastReceiver1, new
IntentFilter("com.myapps.appA");
Inside onRecieve get both the numbers from the intent and Trigger another broadCast with the result i.e.
int num1 = intent.getIntExtra("num1",0);
int num2 = intent.getIntExtra("num2",0);
Intent intent2 = new Intent("com.myapps.appB");
intent2.putExtra("sum",num1+num2);
YourActivity.this.sendBroadcast(intent2);
Now Register the reciever for Broadcast2 Inside your App A, you can do this in onCreate of its Main activity.
private BroadcastReceiver broadcastReceiver2;
...
broadcastReceiver2 = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
}
};
registerReceiver(broadcastReceiver2, new
IntentFilter("com.myapps.appB");
Inside its OnRecive() get the result
int sum = intent.getIntExtra("sum",0);
Most importantly don't forget to unregister the receivers in onStop on the activity
I created a Broadcast Receiver (BR) in a service that will react to incoming SMS with specific number and body. I only need it to receive for a few seconds/minutes after user action, that's why I didn't registered it in manifest or activity (user may close it). BR has two parts, automatic (which works fine) and manual which should launch MainActivity and start a Dialog. I know that Dialog can't be started from BR and thats why I created a Listener, but my problem is that it is always null after service starts. It has value in onCreate of my MainActivity, but when service starts it changes to null, and I understand why (serivce re-initalize the Listener listener). I even tryed to put initialised listener value to SharedPrefs and restore it after, but when I try to store it with json it only stores null again. So how do I make my listener != null??? These are the relevant parts of my code:
MainActivity
onCreate {
SMSService smsReceiver = new SMSService();
smsReceiver.setListener(new SMSService.Listener() { //here listener from service is != null
#Override
public void onTextReceived(String s) {
dialogS(s); // totaly different dialog
}
});
...
mDialogBuilder = new AlertDialog.Builder(this);
...
.setPositiveButton(new OnClick...
Intent servisIntent = new Intent(MainActivity.this, SMSService.class);
startService(servisIntent);
...
}
SMSService
private Listener listener; // and here it get null which is the problem
public int onStartCommand(Intent intent, int flags, int startId) {
...
SMSReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MainActivity.class);
context.startActivity(i);
if (listener != null) {
listener.onTextReceived("4333");
}
}
void setListener(Listener listener) {
this.listener = listener; }
interface Listener {
void onTextReceived(String text);
}
Btw I also tried to put smsReceiver.setListener block of code in my Dialog .setPossitive onClickListener after calling startService hoping it would initiate after service but nothing
Installing a listener mechanism with setter method in service is bad practice. You can use ResultReceiver to receive callback results from service. It is Parcelable, so it can be passed in an intent before service started
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.
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.
public class SessionManager extends BroadcastReceiver{
Date timeOff;
Date timeOn;
#Override
public void onReceive(Context context, Intent intent) {
if( "android.intent.action.SCREEN_OFF".equals(intent.getAction())) {
Log.i("MobileViaNetReceiver", "Screen off - start time to end session");
timeOff = Calendar.getInstance().getTime();
}
if( "android.intent.action.ACTION_SHUTDOWN".equals(intent.getAction())) {
// DO WHATEVER YOU NEED TO DO HERE
Log.i("MobileViaNetReceiver", "Shut down - log off user");
DbAdapter_User db = new DbAdapter_User(context);
db.open();
db.handleLogout();
db.close();
}
if( "android.intent.action.SCREEN_ON".equals(intent.getAction())) {
timeOn = Calendar.getInstance().getTime();
long diffInMs = timeOn.getTime()-timeOff.getTime();
// convert it to Minutes
long diffInMins = TimeUnit.MILLISECONDS.toMinutes(diffInMs);
if ((int) (diffInMins) > 15) {
//log out user
Log.i("MobileViaNetReceiver", "User inactive for 15 minutes - logout user");
DbAdapter_User db = new DbAdapter_User(context);
db.open(); // ******* HERE *************
db.handleLogout();
db.close();
} else {
Log.i("MobileViaNetReceiver", "User still active");
}
}
}
When the screen is turned ON I am checking if the user has turned scrren off for more than 15 mins, if yes, logout him. And go to LonIn screen.
I want to start an intent when I call that handleLogout() (marked * HERE **)
Can I do that when class extends BroadcastReceiver ? If no, what else can I do ?
You need to remember intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Blessings and happiness to the Google engineer who wrote the followint error message "E/AndroidRuntime( 2339): java.lang.RuntimeException: Unable to start receiver android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?"
public class AgeingAutoStartBroadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent(context,MyMainClass.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
);
}
}
Thanks to everyone who writes helpful messages here
Yes, you can do that. You just use the context that was passed into your onRecieve function when you're creating your Intent. Once you have the Intent, make this call:
Context.startActivity(yourIntent);
You certainly can. Try
Intent yourIntent = new Intent(context, YourActivity.class);
startActivity(yourIntent);
If you want to do it in your handleLogout method, pass in the Context.
private method handleLogout(Context context) {
...
}