I want create an app that works like that:
When I take my device, I will unlock it and this app will be alredy opened. Then this app will show a compromise term of use the tablet. There will be two options, allow or refuse. If I choose allow, the app will finish; when I choose refuse, nothing happens until I choose allow.
Then, after I use my device, it will lock, then... I will unlock, and the app will be there again!
How can I put this app to auto-run when I unlock the device?
Add the receiver in menifest file
<receiver android:name=".ScreenReceiver">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
Create a broadcast receiver which works to open app when phone is unlocked.
public class ScreenReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
System.out.println(intent.getAction());
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT))
{
Intent intent1 = new Intent(context,MainActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
}
Already asked, and answered: android unlock screen intent?
You'll need to listen for that intent and then you can launch your app.
Related
I uninstall an app programatically in Android, using an Intent, like you see below:
Uri packageURI = Uri.parse("package:"+packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);
Which redirects to a prompt that asks if you want to uninstall the app.
Usually after that you can see a toast in the bottom of the screen saying the app was uninstalled. But I want to be able to be notified so I can remove an uninstall button in a view.
How can I know in the code when the uninstall of the package has been completed? Or if an error occurred? Or even, if user clicked "ok" to uninstall or "cancel" if he changed his mind, how can I know?
Is it possible to know any of this? Is there an alternative way to uninstall a package (without being a system app) and be notified?
Thank you for reading. Lemme know if you need any more information.
Well I ended up finding a solution, when a package is removed there is an intent that can be picked up by a receiver.
In my AndroidManifest
<application
<!--...-->
<receiver
android:name=".UninstalledBroadcastReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
</application>
Create a UninstalledBroadcastReceiver class that extends a normal BroadcastReceiver
public class UninstalledBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// handle the intent here
}
}
You can check for instance if your package name of the app that was installed matches your own and do something with it. In my case, I put some static fields in the UninstalledBroadcastReceiver, including an interface so that I could perform some callbacks. Don't think you can pass fields in the constructor, since the object is created when the intent is received.
For check screenshot and detail, click here
Please give related suggession or code for how to add automatic enable auto start for my app , please check here attached screen shot thanks in advance.
Try this...it's working for me. It will open the screen to enable autostart.
String manufacturer = "xiaomi";
if(manufacturer.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
//this will open auto start screen where user can enable permission for your app
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
startActivity(intent);
}
Few OEMs including (RedMi) customizes the stack ROM for battery/memory optimization and have blocked the "onDestroy()" and "onTaskRemoved" callbacks. As an user you can prevent the App's service from killing by locking the App. Or, White listing the app by enabling "Autostart" setting for the App. Programmatically you can prompt the user to enable the Autostart for the App Please find details here
Please note: I have tested the Autostart enabling programatically on few devices but found that it does not works always. Please check above link to see the possible options.
First of all you need a permission at the manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Still in manifest you need to add a brodcast receiver within your
<application>
element:
<receiver android:name="net.example.MyOwnBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
after that in your Class "MyOwnBroadcastReceiver"
package net.example;
public class MyOwnBroadcastreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
You can get more help on the following links:
http://blog.gregfiumara.com/archives/82
http://techblogon.com/android-start-service-on-boot/
I hava a question about Android Receiver.
I'm possible to change System app.
B is the first app, when user turns on the power. But the problem is when user chooses FACTORY Mode(like setting language, google id...), B App has to be started finishing A App setting. That's why use android:enabled="false"and A App trigger B app. But not working.
I think "android.intent.action.BOOT_COMPLETED" send just one time after booting, so after changing enable receiver B app, it's not working. Is it right?
Please can you give me some advise?
A App
PackageManager pm = getPackageManager();
ComponentName compName = new ComponentName("com.test.myapp", "com.test.myapp.receiver");
pm.setComponentEnabledSetting(compName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);
B App AndoidManifest.xml
<receiver
android:name="com.test.myapp.receiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
B App
public void onReceive(Context context, Intent intent) {
if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){
Intent startMainActivityIntent = new Intent(context, new.class);
startMainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startMainActivityIntent);
}
Why don't you just start app B from app A directly? Yes, boot completed triggered only once. But you can start app B without any receivers, look here for example
I develop one apps mobile number lock, i want whenever mobile on,or restart or switch on ,or on from the top/left/right button placed on mobile in short whenever mobile screen on my lock activity call, i have no idea to how to call activity at time of mobile on please any one give some related example to start activity at first when mobile on. so my lock dispay to user and then enter number password and lock open ...thanks in advance..
Following is working for me :
enter code here :
public class BootReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null) {
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Intent s = new Intent(context,ViewPagerMainActivity.class);
s.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(s);
}
}
}
}
and in menifist file add follwing:
enter code here :
<receiver android:name=".BootReciever">
<intent-filter android:enabled="true" android:exported="true">
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
There are multiple ways, to achieve this. One way is you can go through Broadcasting a message for your app, after the boot of your mobile. Try reading:
http://developer.android.com/reference/android/content/BroadcastReceiver.html
Also have look at this thread, this will solve your problem.
How to launch activity on BroadcastReceiver when boot complete on Android
I need my android app to be in background mode after a phone restart/power on.
Currently I am using the following code, so that my app successfully gets launched after a phone restart/power on.
AndroidManifest.xml:
<receiver android:enabled="true" android:name="my_package.BootUpReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
BootUpReceiver.java:
public class BootUpReceiver extends BroadcastReceiver
{
private static SharedPreferences aSharedSettings;
#Override
public void onReceive(Context context, Intent intent)
{
aSharedSettings = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
boolean isUserLoggedIn = aSharedSettings.getBoolean(Key.AUTHENTICATED, false);
if(isUserLoggedIn)
{
Intent aServiceIntent = new Intent(context, MyHomeView.class);
aServiceIntent.addCategory(Intent.CATEGORY_HOME);
aServiceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(aServiceIntent);
}
}
}
As I said above, my app successfully gets launched after a phone restart/power on.
However, after the phone restart/power on, my app was in foreground mode. But I need my app to be in background mode.
Can anyone please say, how to make an app to be in background mode after a phone restart or power on.
I even tried by changing the intent category to
<category android:name="android.intent.category.HOME" />
But no use in it. Can anyone please help me?
Thanks.
I need my app to be just running in background after the phone restart, so that users can select from the minimized app
I think your approach is wrong. All you are trying to do now is to add icon of your app to recent apps list. Your app won't run in background and I think you don't really want it. Am I right?
Recent apps list managed by android and IMHO forcing your app to be in recent apps list is not a very good idea. User will start you app when he wants from launcher or icon on his desktop.
If your broadcast receiver is working fine and app is starting successfully then you can use the below code in your MyHomeView activity's onCreate method to go to the home screen.
Trick is to click HOME button programmatically when app starts.
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
you can pass some variable from the BroadcastReceiver to differentiate a normal request and BroadcastReceiver's request to make the above code conditional.
But if you want to execute it always in background then it would be better to use Service.
It is recommended to change your code to the service to run it in background.
The suggestion which Leonidos replied is correct.
However, Just a workaround for this:
In my BootUpReceiver, I had a seperate boolean flag for this! (Its a bad way. but just a workaround)
SharedPreferences.Editor aPrefEditor = aSharedSettings.edit();
aPrefEditor.putBoolean(Key.IS_DEVICE_RESTARTED, true);
aPrefEditor.commit();
In Oncreate method of MyHomeView:
boolean isDeviceRestarted = aSharedSettings.getBoolean(Key.IS_DEVICE_RESTARTED, false);
if(isDeviceRestarted)
{
SharedPreferences.Editor aPrefEditor = aSharedSettings.edit();
aPrefEditor.putBoolean(MamaBearKey.IS_DEVICE_RESTARTED, false);
aPrefEditor.commit();
moveTaskToBack(true);
}
Thanks