How to pass value from BroadcastReceiver to main activity - android

I want to tell my MainActivity, that it is starting automatically by BroadcastReceiver when boot is completed. It seems to be possible to send over putExtra some values to the MainActivity like this:
public class StartAppAtBootReceiver extends BroadcastReceiver {
private static final String key_bootUpStart = "bootUpStart";
private static boolean bootUpStart = true;
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activityIntent.putExtra(key_bootUpStart, bootUpStart);
context.startActivity(activityIntent);
}
}
}
But how can I receive that value inside my MainActivity?

On the BroadcastReceiver you send the intent to the Activity.
I modified your key to be public so that you can reuse it.
public static final String KEY_BOOTUP_START = "bootUpStart";
On the Activity you process the Intent.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
processExtraData();
}
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
setIntent(intent);
processExtraData()
}
private void processExtraData()
{
Intent intent = getIntent();
// Use the data here.
boolean value = getIntent()
.getBooleanExtra(StartAppAtBootReceiver.KEY_BOOTUP_START, false);
}

In your activity you can get the intent which has started your activiy like:
in onCreateActivity:
Intent intent = getIntent();
Than:
Object value = intent.getExtra("key_bootUpStart");

Related

Activity cant find Bundle in Intent

I am using an IntentService (for the first time), and I intend to return the result of this service by using a Bundle. However, when I do this, the main activity does not find the Bundle, returning null. What could cause this? The string keys match!
The code bellow outputs:
I/System.out: It's null
Main Activity:
public class MainMenu extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//Some stuff here...
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(StorageHandler.TRANSACTION_DONE);
registerReceiver(broadcastReceiver, intentFilter);
Intent i = new Intent(this, StorageHandler.class);
startService(i);
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Bundle extra = getIntent().getBundleExtra("bundle");
if(extra == null){
System.out.println("It's null");
}
else {
ArrayList<String> objects = (ArrayList<String>) extra.getSerializable("objects");
System.out.println(objects.get(0));
}
}
};
}
IntentService:
public class StorageHandler extends IntentService{
public static final String TRANSACTION_DONE = "xx.xxxxx.xxxxxx.TRANSACTION_DONE";
public StorageHandler() {
super("StorageHandler");
}
public void onCreate(){
super.onCreate();
}
#Override
protected void onHandleIntent(Intent intent) {
notifyFinished();
}
private void notifyFinished(){
ArrayList<String> objects = new ArrayList<String>();
objects.add("Resulting Array here");
Bundle extra = new Bundle();
extra.putSerializable("objects", objects);
Intent i = new Intent();
i.setAction(xx.xxxxx.xxxxxx.StorageHandler.TRANSACTION_DONE);
i.putExtra("bundle", extra);
StorageHandler.this.sendBroadcast(i);
}
You're attempting to retrieve the data from the wrong Intent.
Change:
Bundle extra = getIntent().getBundleExtra("bundle");
To:
Bundle extra = intent.getBundleExtra("bundle");
The Intent that contains your data is supplied as one of the parameters of the BroadcastReceiver's onReceive() method.
you are using getIntent() to retrieve the broadcasted intent. This is wrong. The intent you have to use is the former paramter of onReceive. Change
Bundle extra = getIntent().getBundleExtra("bundle");
with
Bundle extra = intent.getBundleExtra("bundle");
Simply use this in your Activity:
In onResume callback you should register registerReceiver(broadcastReceiver, intentFilter);
And in onPause callback you should unregister this receiver. In your receiver use this:
Bundle extra = intent.getBundleExtra("bundle");
In your service use this code:
Intent i = new Intent(TRANSACTION_DONE).putExtra("bundle", extra);
this.sendBroadcast(i);
More information , see This Answer
Dont forget this in your onCreate MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//All your code here
}
I say this because I dont see that code line in your method!

Sending Intent from BroadcastReceiver class to currently running activity

I have a class which extends BroadcastReceiver. On receiving a SMS, I would like to pass information to my main activity class to display the text in a box (Append, if already text is present).
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
Intent i = new Intent(context, MainActivity.class);
i.putExtra("updatedString","Hello");
context.startActivity(i);
}
}
MainActivity.java
public class MainActivity extends Activity{
private TextView results;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle extras = getIntent().getExtras();
if(extras!=null){
results = (TextView) findViewById(R.id.results);
results.setVisibility(View.VISIBLE);
results.append(extras.getString("updatedString"));
}
}
I have only one activity class (MainActivity.java). However When i do this I get an exception Unable to pause Activity.
You have three ways:
1) You can define your broadcast inside your MainActivity like this:
in onCreate()
registerReceiver(smsReceiver, new IntentFilter(SMS_RECIEVED));
and define smsReciver in MainActivity
private BroadcastReceiver smsReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//you can update textBox here
handler.postDelayed(sendUpdatesToUI, 10);
}
};
define a runnable to update UI
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
update();
}
};
and update method
private void update(String text) {
textView.setText(textView.getText().toString() + text);
}
2) Register a receiver between your Activity and BroadCastReceiver
3) Start your Activity with new Intent to update current open Activity
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("Key", "text");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
UPDATE :
explain method 2
MainActivity.class
in onResume()
registerReceiver(broadcastReceiver, new IntentFilter(SmsReceiver.BROADCAST_ACTION));
in onDestroy()
unregisterReceiver(broadcastReceiver);
local broadCast (broadcastReceiver, in MainActivity.class)
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
updateUI(intent);
}
};
private void updateUI(Intent intent) {
String text = intent.getStringExtra("key");
textView.setText(textView.getText().toString() + text);
}
SmsReceiver.class
global attribute
public static final String BROADCAST_ACTION = "your.package.name.displayevent";
private final Handler handler = new Handler();
Intent intent;
Context context;
in onReceive()
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 10);
this.context = context;//you can retrieve context from onReceive argument
this.intent = new Intent(BROADCAST_ACTION);
define two method
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
display();
}
};
private void display() {
intent.putExtra("key", text);
context.sendBroadcast(intent);
}
Modify your code as below.
public class SmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
Intent i = new Intent(context, MainActivity.class);
i.putExtra("updatedString","Hello"); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(i);
}
}
public class MainActivity extends Activity{
private TextView results;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle extras = getIntent().getExtras();
if(extras!=null){
results = (TextView) findViewById(R.id.results);
results.setVisibility(View.VISIBLE);
results.append(extras.getString("updatedString"));
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//handle your intent here.Note this will be called even when activity first created.so becareful to handle intents correctly.
}
}

Getting bundle with intent null, Intent issued from helper object

I am sending an intent to start an activity from broadcast receiver using a helper object as follows:
Broadcast receiver code:
MyReceiver extends BroadcastReceiver{
onReceive(){
new Helper(this).startMyActivity();
}
Code in my helper object:
Helper{
private Context myContext;
public Helper(Context c){
myContext=c;
}
public void startMyActivity(){
Intent i=new Intent(myContext,MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("code", 1);
myContext.startActivity(i);
}
}
But when I try to extract out the bundle in the activity MyActivity, I get null value:
MyActivity extends Activity{
onResume(){
Bundle b=getIntent().getExtras();
int c=b.getInt("code");
}
}
Why am I getting the bundle null?
The problem is you need to override onNewIntent(Intent) in the Activity and explicitly tell it to use the new Intent
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
I think,
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
is the problem, . Comment the line and try it.
or you can get the value from
Intent receiverIntent = getIntent();
int k = receiverIntent.getIntExtra(name, defaultValue)
Try to use
int c = getIntent().getIntExtra("code", 0);

sending intent extras from service to activity

I have a service that listens for (ON_BATTERY_CHANGE), then onReceive service sends a Broadcast to My MainActivity. The problem is that I somehow can't get them from service to my main activity. Code: Main Activity:
public class MainActivity extends Activity
private BroadcastReceiver batteryReceiverService;
private TextView text2;
....
protected void onCreate(Bundle savedInstanceState) {
text2=(TextView)findViewById(R.id.TV_text2);
batteryReceiverService = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
text2.setText("left: "+intent.getStringExtra("H")+" hours "+intent.getStringExtra("M")+" minute(s)");
Log.e("text2","text2 HHH " +intent.getStringExtra("H")); //log shows 0
Log.e("text2","text2 MMM " +intent.getStringExtra("H")); // log shows 0
}
};
registerReceiver(batteryReceiverService, new IntentFilter(UltimateBatterySaverService.BROADCAST_ACTION));
....
#Override
protected void onDestroy() {
unregisterReceiver(batteryReceiverService);
super.onDestroy();
}
Service:
public class UltimateBatterySaverService extends Service {
private Intent intent;
static final String BROADCAST_ACTION = "lt.whitegroup.ultimatebatterysaver";
private BroadcastReceiver batteryLevelReceiver;
....
public void onCreate() {
super.onCreate();
intent = new Intent(BROADCAST_ACTION);
}
#Override
public void onDestroy() {
unregisterReceiver(batteryLevelReceiver);
super.onDestroy();
}
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
batteryLevelReceiver = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent){
// Receiving data, calculating and etc
averageChargingH=timeAllInHours;
averageChargingM=timeAllInMinutes;
// to put extras and send broadcast
does();
......
public void does(){
String strLong = Long.toString(averageChargingH);
String strLong2 = Long.toString(averageChargingM);
Log.e("cccccc","strLong h "+strLong); // getting good value not 0(everything ok)
Log.e("cccccc","strLong2 m"+strLong2); // getting good value not 0(everything ok)
intent.putExtra("H", strLong);
intent.putExtra("M", strLong2);
sendBroadcast(intent);
}
Any ideas why my information is not transfered correctly?
The does() method seems to be using variables in the same scope as onReceive so I'm guessing that the intent variable in does() is actually the Intent passed in from onReceive.
Try adding some logging before sending the broadcast to check if the action of the intent is correct, or simply create the broadcast intent in the onReceive method and name it intent2.

Is it possible to call putExtras() and getExtra() without starting a new activity?

I'm having two activities which are displayed side by side on one screen through a fragment for each activity. I want to send a String when I click a button on one of the activity, and then the other activity should retrieve the String I sent.
I know that it's possible to do that with the classic way setString() and getString() methods. But I'm wondering, could it be accomplished with putExtra() and getExtra() without opening the targeted activity on a new screen?
You can have both activity's intent as global variable and try using putExtra() from one activity and then getExtra() by another activity.
For Example,
public class activityOne extends Activity
{
public static Intent intent = null;
#Override
protected synchronized final void onCreate(final Bundle savedInstanceState)
{
intent = getIntent();
}
}
public class activityTwo extends Activity
{
public static Intent intent = null;
#Override
protected synchronized final void onCreate(final Bundle savedInstanceState)
{
intent = getIntent();
}
}
Now wherever you want you can access their intent like activityOne.intent,activityTwo.intent followed by null check
You can send the intent as a broadcast and have the receiving fragment register to that broadcast on its start (and unregister on its destroy).
Sending fragment:
Intent intent= new Intent(PARAM_TRANSFER);
intent.putExtra(key, extra);
sendBroadcast(intent);
Receiving fragment:
register:
IntentFilter intentFilter = new IntentFilter(PARAM_TRANSFER);
registerReceiver(mReceiver, intentFilter);
unregister:
try {
unregisterReceiver(mReceiver);
} catch (Exception e) {}
getting the param:
private class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
intent.getExtra(key);
}
}

Categories

Resources