This question already has answers here:
start a remote service and receive a result in Android
(2 answers)
Closed 9 years ago.
How I can get response from Activity, which start from my Service?
I start my Activity in Service's code:
Intent intent = new Intent( context, ActivityMy.class);
intent.putExtra( "iii", iii);
context.startActivity( intent);
startActivityForResult available from Activity but no Service.
How I can get response from Activity?
Update: I need not only send one value from Activity to Service, I need run immediately my code when Activity is finished and return result.
You can create a custom Class that extends of Application and set it in Manifest (just set the android:name attribute in <application>). In this class you can put a static variable called activity and set everytime you change the activity. So in your Service you will can access the activity by using MyApp.activity
The simplest way is to use BroadcastReceiver to send broadcast from Activity to Service.
Related
This question already has answers here:
How to bring an activity to foreground (top of stack)?
(10 answers)
Closed 4 years ago.
I have an app that needs to run 24/7.
I know that android OS kills the process of my app after some time in the background, so i have a Foreground service which is sticky.
Now the thing is, that i want the service to check if my app is running, and if it is not, then i want the service to start the app. Is it possible?
After onStop your activity will be invisible. If you want to see that your activity running, i.e., on the foreground;
public boolean isForeground(String myPackage) {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager.getRunningTasks(1);
ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
return componentInfo.getPackageName().equals(myPackage);
}
If it is not in the foreground, you can use below otherwise;
Intent i = new Intent(this, MyMainActivity.class);
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
Note; another method, you can send message to your service from your activity about its status
or, use idolon's idea]2;
What kind of app is it? I think such types of apps can be very annoying for users.
I am not sure what exactly functionality your app have, but are you sure that you can't achieve that same functionality with using AlarmManger or JobScheduler ?
I have a class that listens to SMS broadcast and, it then open an activity in the same application to display the message. This is part of a larger project.
Since the broadcast class does not extend the activity class, calling an Activity with intent requires that I flag it with Intent.FLAG_ACTIVITY_NEW_TASK.
Now the Intent has an extra (the SMS message and some parameters as a single string) which i want to send with the intent. All went well but the Extras are not sent with the intent.
Here are my code
From the broadcast class
//After getting the message into String S and doing some process
Intent inte = new Intent(context.getApplicationContext(), StartApp.class);
inte.putExtra("messageAsString", S.toString()); //Where S is declared somewhere
inte.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(inte);
From the activity recieving the Intent in the onCreate() method
if(getIntent().hasExtra("messageAsString")) {
String message = getIntent().getStringExtra("messageAsString").toString();
Toast toast = Toast.makeText(this, "Received SMS: " + message , Toast.LENGTH_LONG);
toast.show();
}
The application may have already running, the home button has been pressed. Either way, what I want to achieve is that as the registered reciever receives the message, I want to start an activity automatically and pass the message and its meta data to it.
I know I'm missing something somewhere but will appreciate your swift help
You also need to handle onNewIntent(), which may be called if the activity already exists and startActivity() brings an existing instance back to the foreground. Your extra is probably in there.
Okay I got my Application and my MainActivity. When the user presses my app icon my application launches and MainActivity gets created. Now in onCreate() with getIntent() method can i get the Android System component(the class name) which started my activity??
You can get class name by using this code.getClass().getName()
In getIntent() you receive parse variable throw intent.
I always get bundle value null when I pass data from activity to broadcast receiver.
My receiver will start on boot up.
This is the code in my activity class
Intent intent= new Intent();
intent.setAction("android.intent.action.BOOT_COMPLETED");
intent.putExtra("test", "test");
sendBroadcast(intent);
This is the code in my receiver class:
String testValue = intent.getStringExtra("test");
Your code in activity will never be called upon booting up. System invokes onReceive() with its own intent. You can check this by putting some logs in activity code - this log will not be printed in logcat.
I have a app with a main activity, a graph activity an a Bluetooth ConnectListenerImpl.
The main activity search for a bluetooth device and the Bluetooth ConnectListenerImpl connect with the device and send data Message to the main activity with a Handler.
And that the main activity display the data.
Now i want to display the data in the graph activity which is a child of the main activity.
The start of the child activity.
bGrafiekShow = true;
Intent intent = null;
intent = new Intent(MainActivity.this, GraphActivity.class);
startActivity(intent);
Now is my question how can i do that and what is the best way?
Little extra information. The main activity put the data from the Bluetooth in a other class this class make some calculation. After the calculation is finish the main activity put the result on the screen.
The Bluetooth device sends the data every one second.
Now I want the calculated data plot in a time graph in a child activity. But how can i send the data to the child activity.
You can pass data thought Activities
as map of data presented like Bundle
with putExtra() methods of Intent
with Serializable or Parceable interface.
Or you can use ResultReceiver class. You can combine it with IntentService instead of Handler. In Service you will do your work and any update will be sent into ResultReceiver and receiver will update UI.
Put the device ID in your intent or in a bundle.
intent.putExtra("DEVICE_ID", deviceId);
When starting your second activity, retrieve device Id and connect your "child" second activity to the bluetooth via ConnectListenerImpl.