Restart Activity via service - android

I have 1 service about mqtt receiver. When application receive message from mqtt server, it will start newA activity. this newA activity work fine but the problem is if I started newA activity before i receive message, nothing gonna happen. I tried
private newA na;
public void MessageArrive {
na.onDestroy(); //on newA activity onDestroy() is contain finish();
Intent runs = new Intent(getBaseContext(), newA.class);
runs.putExtra("data", arrMsg);
runs.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(runs);
}
but it's not work.
have any idea?
thanks in advance.

try use boardcast and on receive to listen boardcast should solve this problem.

You can override onNewIntent in your activity

Related

Closing an Activity in Broadcast Receiver

I have a broadcast receiver to check the internet connectivity, in which i have validated if no internet connection I am displaying an error screen through intent. Now i need to close the error screen when the network is reconnected and display the previous screen from which the network was gone.
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
isConnected = intent.getBooleanExtra(ConnectionDetector.CONNECTED_KEY, false);
if(isConnected){
System.out.println("A1");
finish();
}
if (intent.getAction().equals(CONNECTION_BROADCAST_ACTION)) {
System.out.println("A2");
No_Network.this.finish();
}
}
};
This is my code inside the error screen activity. How to close the activity here? finish() is not working.
How to close the activity here? finish() is not working
It is "not working" because you are not using it correctly. The correct approach is to notify the activity and tell it to finish() itself. You can do that by sending intent with "magic" code of yours directly to your activity (if it is in singleTop launch mode then startActivity() + onNewIntent() would suffice). Alternatively use event bus for communication.

Service can't control activity UI?

I have problem when I control Activity UI from service. It doesn't work.
class MainActivity
public void showNotice() {
Log.d("log", "can't connect to server");
tvText.setText(notice);
tvTex.setVisibility(View.VISIBLE);
pbDialog.hide();
}
I call showNotice method in my service:
((MainActivity) mContext).showNotice();
But it only show log "can't connect to server".
tvText doesn't change anything, not change text, not visible. pbDialog does'n hide?
Can i help me resolve it? Many thanks.
A service runs in its own background thread, the UI can be modified only from the UI Thread which is pretty much with the main activity.
You can try doing this, you can broadcast an event from the service when you want to hide the dialog box. Have a broadcast listener registered for this which would handle the UI modification. You should probably be using a LocalBroadcastManager and give your broadcast a unique name.
In your mainActivity, use
registerReceiver(<receiver instance>, <broadcast name>)
in the onStart or onCreate method.
This will setup your mainActivity to listen to the broadcasts, next you need to define your broadcast receiver which is the in the above register call.
BroadcastReceiver receiver;
receiver = new BroadcastReceiver() {
public void onReceive(Context c, Intent i) {
//Modify the UI, in this case hide the dialog box.
}
}

How to terminate Android app from within service?

On receiving a certain event in my Android service, I want to terminate the app from within the service. I know I can call finish in an activity to end it.
Also I understand that service will call stopSelf() on itself to end itself. But I need to terminate the entire app including any particular activity of the app that was visible at that time.
Any ideas?
Try this:
Process.killProcess(Process.myPid());
You can create an BaseActivity with BroadcastReceiver to close all Activities by registering it in onCreate() of BaseActivity and extending all your Activities with BaseActivity.
public static final String EXIT_APP_ACTION = "EXIT_APP_ACTION";
BroadcastReceiver ExitAppBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
Register it in onCreate() for the BaseActivity in your Application using,
registerReceiver(ExitAppBroadcastReceiver, new IntentFilter(EXIT_APP_ACTION));
Then you can fire this BroadcastReceiver from your Service using the Context to close all Activites including Service by calling stofSelf(),
stopSelf()
context.sendBroadcast(new Intent(BaseActivity.EXIT_APP_ACTION));

Some Issues about Using BroadcastReceivers

I want to use BroadcastReceiver in my application as AsyncTask result indicator in different Activities and therefore AsyncTasks too. I think my approach is little wrong or I missed something.
Here what I'm doing: Firstly, during onCreate I registered my receiver as a BroadcastReceiver using the registerReceiver method. My receiver looks like:
protected BroadcastReceiver myReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String msg_for_me = intent.getStringExtra("some_msg");
Log.i("Tutorial", msg_for_me);
}
}
In my application, I've A and B activities. Each have different receivers which getting messages from different tasks. By the way, I must clarify that, A activity starting B activity.
I'm using receiver which in activity A, then activity A starting B using the startActivity. In activity B, I'm executing an AsyncTask and on onPostExecute I'm sending a broadcast with B activity's context. But somehow still A activity's receiver getting message. Both receivers have the same content but have different names.
So here are my issues:
Should I unregisterReceiver when I started new activity on onPause method?
Is BroadcastReceiver that how I'm using, only for one call? Should I register again and again whenever I send any message?
I'm pretty sure I didn't define any receiver to Manifest. I suppose this is what I'm doing wrong. If this is well, how can I use IntentFilter while sending broadcast?
Please let me know if there is uncertain question. Any clues about BroadcastReceiver would be great and appriciated.
Yes, you should unregister broadcast receiver on activity pause. It
is the potential leak.
No broadcast receivers are not for one call.
They are called everytime the broadcast is done for the registered
intents.
You can register the receiver for particular intent on
OnResume like this,
mContext.registerReceiver(iReceiver, new android.content.IntentFilter("android.intent.action.BATTERY_CHANGED"));
Where iReceiver is ,
iReceiver = new IntentReceiver();
private class IntentReceiver extends BroadcastReceiver {
private final String LOG_TAG="IntentReceiver";
#Override
public void onReceive(Context arg0, Intent intent) {
}
}
and unregister the same on OnPause
mContext.unregisterReceiver(iReceiver);

Android kill process from broadcast receiver

Hi i'm looking to kill an activity in my application when the usb is disconnected i have a broadcast receiver and all set up and working fine
public class USBOff extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent(context, SdcardOff.class);
startActivity(intent1);
finish();
}
}
This code however tells me that the finish method and startActivity methods need to be created? why is this not working thank you for any help with my problem
startActivity is a Context method, not a BroadcastReceiver method. finish is an Activity method. Try this:
context.startActivity(intent1);
You can't call finish from a BroadcastReceiver.
I have found an interexting method :
Manfest :
android:launchMode="singleInstance"
<action android:name="com.gr.app.KILLSELF" />
receiver code :
Intent intentInterface = new Intent("com.gr.app.KILLSELF");
mContext.startActivity(intentInterface);
activity onCreate part :
if (intent.getAction().compareTo("com.gr.app.KILLSELF") == 0) {
finish();
};
It will not work in such configuration if you may have many instancesof activity (string android:launchMode="singleInstance" is a problem for you),
I hope it will help
Simple Logic :
If you dont want to kill the activity then do not use finish() And if you want to kill the activity then you should have to call finish();
So remove the finish(); from code and try it out.
Enjoy.

Categories

Resources