I am newbie to android and I am implementing background service im my app, i am starting app on BOOT_COMPLETED process & i am calling the activity to start the app and app come on foreground. But i want to keep the app in background (Minimized) and i want to do slient login in app.
i am using below code for app start on Boot_Complete.
public class bootUpLocation extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
// here we start the service
Toast.makeText(context, "broadcast receiver start for location",
Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(context, LoginActivity.class);
myIntent.addCategory(Intent.CATEGORY_HOME);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
}
Related
i want "start in Background" permission for open incoming and outgoing call screen when app close.
public class CallReciver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("onReceive","=========>>>>");
//start activity
Intent i = new Intent(context.getApplicationContext(), OngoingCallActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(i);
}
}
receiver is opening incoming or outgoing UI screen.but receiver is able to open activity when app in foreground. onReceive method call but receiver not able to open activity when app in background, i try lot's of solution for that but nothing to work.
when manually allow start in background permission it work fine.
so,how to allow start in background permission in MIUI software.Please help me i spend lot's of day for that.sorry for bad english and thanks in advance.
For MI device follow below steps
Intent localIntent = new Intent("miui.intent.action.APP_PERM_EDITOR");
localIntent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
localIntent.putExtra("extra_pkgname", getPackageName());
startActivity(localIntent);
I'm trying to create an autorun service: so that the application launches every time after unlocking the screen, after entering the graphic key or password if it exists (on Android 7,8,9,10). I wrote the code dynamically through the borocast receiver (ACTION_SCREEN_OFF) but it works while the application is on the stack (running) and I want it to always start. The method through registering in the manifest in android 9 already does not work the listeners. How to implement this?
public class WordsBase extends AppCompatActivity {
ScreenReceiver resiverStart;
#Override
protected void onPause() {
super.onPause();
resiverStart= new ScreenReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(resiverStart,filter);
}
}
public class ScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Intent intent1 = new Intent(context, WordsBase.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
throw new UnsupportedOperationException("Not yet implemented");
}
}
I understand that you want to do the following:
If the user unlocks the device, you want to start your app.
Why don't you do the following:
Use the USER_PRESENT receiver (android.intent.action.USER_PRESENT). Please not that you have to register explicitly to this receiver, just registering it in the manifest is not enough
If the respective broadcast is fired, start your app and make sure you are still registered to the broadcast (to have your app started again the next time the user unlocks the device).
I need to make an alarm app that opens on a custom selected time (comes forward on the screen even if the app is closed ). Using alarm manager/ broadcast receiver i managed to show notification , but not to start the app.
You can start any activity like this from broadcast receiver:
public class Example extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, Main.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Is there a way to Resume an app from a BroadcastReceiver? (in the way that the Launcher Resumes or Starts an app)
I want to do this when the device is plugged in (don't worry, this isn't going in the Play store)
It seems like you must use FLAG_ACTIVITY_NEW_TASK or else you receive a RuntimeException for "Calling startActivity() from outside of an Activity context"
public class PowerReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.ACTION_POWER_CONNECTED")) {
final Intent newIntent = new Intent(context, FooActivity.class);
//newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(newIntent);
}
}
}
I need to launch/open one installed apk in my device from a BroadcastReceiver.
Here is the code:
public class C2DMMessageReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.w("C2DM", "Message Receiver called");
if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
Log.w("C2DM", "Received message");
ComponentName toLaunch = new ComponentName("es.mypackage","es.mypackage.myapplication");
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(toLaunch);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
My device receives the broadcast but fails with an unexpected problem.
The code to launch other apk works fine in other part of the application.
Is possible to launch other application from a broadcast?
Thank you very much.
As per my experience , you can not start activity from the C2DM Receiver, I found work around for that, Create one service and start activity from that service, stop service after you start the activity.
Thank You,