I am launching activity from the service based on some value got from the server and the activity will be displayed for the some time and after getting close instruction from the server i need to close that activity,so for that i used following approach but it's not working:
on Service class:
if(((ActivityManager)this.getSystemService(ACTIVITY_SERVICE)).getRunningTasks(1).get(0).topActivity.getPackageName().equals("com")) {
if(((ActivityManager)this.getSystemService(ACTIVITY_SERVICE)).getRunningTasks(1).get(0).topActivity.getClassName().equals("com.CustomDialogActivity")){
Intent dialogIntent = new Intent(getBaseContext(), CustomDialogActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
dialogIntent.putExtra("description", "");
dialogIntent.putExtra("cancelEnabled", false);
dialogIntent.putExtra("close", true);
getApplication().startActivity(dialogIntent);
}
}
and on activity inside onCreate method:
Bundle bundle = getIntent().getExtras();
boolean isClosed = bundle.getBoolean("close");
if(isClosed){
finish();
}
I debugged it and found that control reaches to the onCreate method if(isClosed) condition and executed finish() method also but its not closing the activity.
so Couldn't be able to analyze what wrong I am doing.
Write a broadcast receiver in your CustomDialogActivity like following.
private final BroadcastReceiver abcd = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
Then register it in the same file like the following:
onCreate(){
registerReceiver(abcd, new IntentFilter("xyz"));
}
Unregister it in onDestroy.
onDestroy(){
//unRegister
}
Now,Whenever you want to close that Activity just call like the following.
sendBroadcast(new Intent("xyz"));
Hope this help.
You must add the receiver into the manifest for your activity, For example :
<activity android:name="MyActivity">
<intent-filter>
<action android:name="xyz" />
</intent-filter>
</activity>
Try to send broadcast using context.
context.sendBroadcast(new Intent("xyz"));
Answer from Jainal is Working for me.
Related
Well, my question is just as simple as in the title:
Is there a way to call finish() on all running activities inside application ?
So i need a way to completly shut down my app.
You can achieve that with a BroadCastReceiver:
In every activity put:
private BroadcastReceiver mLoggedOutReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
Register receiver in onCreate:
registerReceiver(mLoggedOutReceiver, new IntentFilter(Preferences.INTENT_ACTION_LOGGED_OUT));
on onDestroy:
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mLoggedOutReceiver);
}
on manifest for every activity and the following filter:
<intent-filter>
<action android:name="com.example.intent.action.LOGGED_OUT" />
</intent-filter>
I have the intent Action declared in Preference class like this:
public static final String INTENT_ACTION_LOGGED_OUT = "com.example.intent.action.LOGGED_OUT";//logout
And finally all you have to do is call sendBroadcast from an exit button or something:
sendBroadcast(new Intent(Preferences.INTENT_ACTION_LOGGED_OUT));//logout
Hope this helps.
android.os.Process.killProcess(android.os.Process.myPid());
this code can shut down your app.
NO
but you can exist hole application like System.exit(0); but it is not the recomanded way..as android not design to exit the application.
I have an Activity that I display as modeless when the phone rings (over the phone app). I would like to finish the Activity when either of the following events occur. The first is if I touch anywhere outside the Activity (this is not a problem), the second is if the ringing stops. I am listening for IDLE_STATE in the broadcast receiver but I am not sure on how to call the finish on the activity when I see it. The receiver is not registered by the activity but by the Manifest.xml
write the code in your receiving broadcast now this will send another broad cast with the intent named "com.hello.action"
Intent local = new Intent();
local.setAction("com.hello.action");
sendBroadcast(local);
Now catch this intent in the activity with you want to finish it and then call the super.finish() on the onReceive method of your receiver
like this
public class fileNamefilter extends Activity {
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter();
filter.addAction("com.hello.action");
registerReceiver(receiver, filter);
}
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
public void finish() {
super.finish();
};
}
this will finish the activity
What if you register another broadcast receiver from the activity. Then, when you want to kill it, send a broadcast message from the broadcast receiver that you mentioned.
I actually ended up adding a PhoneStateListener in the Activity to listen for IDLE_STATE.
After a long R&D over internet i have solved my problem. The below code is helpful if you start an activity from broadcast receiver and
clear all activity stack instance.
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
moveTaskToBack(true);
}
}
I used this code to solve my problem. finish is the method of Activity, so call Activity and context like:
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
Log.e(TAG, "Inside EXTRA_STATE_IDLE");
((Activity) arg0).finish();
}
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.
I am writing my first service. My activity works well, but when I call my service, it doesn't.
It looks like it's onCreate() is not getting called.
My service code:
public class NeglectedService extends Service {
public static final String MY_SERVICE = "android.intent.action.MAIN";
public void onCreate() {
Toast.makeText(this, "Service onCreate...", Toast.LENGTH_LONG).show();
}
}
I am not even getting the Toast message.
Here is my activity
startService(new Intent(NeglectedService.MY_SERVICE));
My manifest
action android:name="android.intent.action.MAIN"
Did you enter something like
<service android:name=".subpackagename.ServiceName"/>
into your Android Manifest xml file?
Seeing as the NeglectedService.MY_SERVICE is just a string, in your startService call you're essentially calling:
startService(new Intent("android.intent.action.MAIN"));
Clearly that doesn't have any reference to your particular service and isn't what you want. Instead, either register the service for particular intent filters and include those in your intent, or call it by class:
startService(new Intent(this, NeglectedService.class));
Call your Service using an Explicit intent, instead of using an implicit action string, which should be more unique anyway. In other words, use this in your Activity code:
startService( new Intent(this, NeglectedService.class) );
Hope that helps!
In my app, I have an Activity from which I want to start a Service. Can anybody help me?
Add this in your code
Intent serviceIntent = new Intent(this, ServiceName.class);
startService(serviceIntent);
Dont forget to add service tag in AndroidManifest.xml file
<service android:name="com.example.ServiceName"></service>
From the Android official documentation:
Caution: A service runs in the same process as the application in
which it is declared and in the main thread of that application, by
default. So, if your service performs intensive or blocking operations
while the user interacts with an activity from the same application,
the service will slow down activity performance. To avoid impacting
application performance, you should start a new thread inside the
service.
The application can start the service with the help of the Context.startService method. The method will call the onCreate method of the service if service is not already created; else onStart method will be called. Here is the code:
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.testApp.service.MY_SERVICE");
startService(serviceIntent);
First create service from android Manifest.xml file (i.e from application tab) and give some name to it.
On the activity on some event like click or touch to include the code from the service:
public void onClick(View v)
{
startService(new Intent(getApplicationContext(),Servicename.class));
}
If you want to stop the running or started service then include this code:
public void onclick(View v)
{
stopService(new Intent(getApplicationContext,Servicename.class));
}
The API Demos have some examples that launch services.
Use a Context.startService() method.
And read this.
in kotlin you can start service from activity as follow :
startService!!.setOnClickListener { startService() }
private fun startService(){
startService(Intent(this, HitroService::class.java))
}
If you want to start a service and it should run in background use START_STICKY in your corresponding service.
You can start service with on boot also,
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
And create receiver,
<receiver android:name=".auth.NotificationBroadcast" android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
In Brodcast Receiver add,
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("BroadcastReceiverBroadcast--------------------ReceiverBroadcastReceiverBroadcastReceiver----------------BroadcastReceiver");
if (intent != null) {
String action = intent.getAction();
switch (action) {
case Intent.ACTION_BOOT_COMPLETED:
System.out.println("Called on REBOOT");
// start a new service
startService(new Intent(getApplicationContext(),Servicename.class));
break;
default:
break;
}
}
}
And your service is like,