My application works as a launcher and also it starts on startup. However, something is wrong with it. For instance, I install my application on device, and open it by selecting Always button (as default launcher). There is no problem until here. However, if I reboot my device (it opens on startup, as I said before), the application opens. But when I want to close it, I cannot do that. It opens again.
This is my Manifest file:
<receiver android:enabled="true" android:name=".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>
<activity
android:name="com.comeks.cocuktablet.Main"
android:label="#string/app_name"
android:launchMode="singleInstance"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This is BootUpReceiver.java:
public class BootUpReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent i = new Intent(context, Main.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
You can use the PackageManager to clear your own application of its defaults, put this inside your onCreate():
PackageManager pm = getPackageManager();
pm.clearPackagePreferredActivities("com.your.package.name");
and fill in your own package name. That should clear the launcher default from your app, the next time they press home button they should be shown the choices of which app to use.
Related
I use activity-alias to dynamically change the icon of my application, resulting in my application disappear in the recent apps.
Manifest
<activity-alias
android:enabled="false"
android:icon="#mipmap/ic_launcher_11"
android:label="#string/app_name"
android:name=".ui.launcher_d_eleven"
android:targetActivity=".ui.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity-alias>
private void enableComponent(PackageManager pm, String launcher) {
ComponentName newComponent = new ComponentName(this, launcher);
pm.setComponentEnabledSetting(newComponent,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
private void disableComponent(PackageManager pm, String launcherAlias) {
LoggerUtil.e("zkx disable Launcher = " + launcherAlias);
ComponentName deComponent = new ComponentName(this, launcherAlias);
pm.setComponentEnabledSetting(deComponent,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
}
Hitting it a bit late but I got a solution for this, you need to add a default alias pointing to the MainActivity without changing the icon and remove the Launcher property in the Main Activity's intent-filter manifest.
So your MainActivity would look like this
<activity
android:name=".MainActivity"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
And add a default alias that looks like this
<activity-alias
android:name=".Default"
targetActivity = ".MainActivity"
enabled = true>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
And then when you don't enable/disable your MainActivity, you disable the alias and substitute it with another alias
That's what worked for me
I was study about Firebase's click_action use to open activity on notification click when app was killed. It's work fine! But after closing app open app normally(Not from notification) but its open same activity as specified in click_action.
Here is the code in manifest for launch screen :
<activity
android:name=".activity.SplashScreen"
android:screenOrientation="portrait"
android:theme="#style/LoginScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
And for Firebase notification :
<activity android:name=".activity.RequestsActivity">
<intent-filter>
<action android:name="RequestsActivity" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
Code for Application exit when get back to home screen then :
#Override
public void onBackPressed() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
System.exit(0);
}
Flow of current screen
Notification->Click->RequestActivity->Back_Click->HomeScreen->BackClick->Exit_App->Reopen_App->RequestActivity(But it must be HomeScreen!).
I am trying to detect the uninstall action of my application. Till now, I have got a specific code that catch the uninstall action and inflate an Activity. Here is the code:
Manifest:
<activity
android:name=".UninstallActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.DELETE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="package" />
</intent-filter>
</activity>
I have created a simple Activity called UninstallActivity and It works fine. When the user try to uninstall the app this Activity has been inflated.
I am trying to listen on those intents with a Receiver instead of Activity but I have failed to get this action. My code is:
Manifest:
<receiver android:name=".PackageUninstallReceiver" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.DELETE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="package" />
</intent-filter>
</receiver>
PackageUninstallReceiver:
public class PackageUninstallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("uTag", "In the PackageUninstallReceiver onReceive()");
if (intent.getAction().equals(Intent.ACTION_DELETE) && intent.getDataString().contains(context.getPackageName())) {
Log.d("uTag", "Uninstallation is being happened....");
}
}
}
First, is it possible to listen to this Intent with the receiver?
If yes, what is wrong with my code?
The Intent used to start an Activity (in this case, an Intent to VIEW or DELETE a PACKAGE) is a completely different thing from a braodcast Intent. They share some of the same properties, but are still completely different things. a broadcast Intent will never start an Activity and an Intent used to start an Activity will never be seen by a BroadcastReceiver.
Therefore, the answer to your question
First, is it possible to listen to this Intent with the receiver?
is "no".
The actions you are listening are generic and could be applied in any context with a different schema. What you should be listening to is the package changing.
<receiver android:name="PackageChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
I have an application where it is going to be the home screen, default launcher. I am implementing this by using the CATEGORY_LAUNCHER in my intent and CATEGORY_HOME in my manifest file, the home activity has ACTION_MAIN and CATEGORY_HOME. Because there are multiple homes set then android prompts the user to select one with the optional extra of selecting a default one. There are many default apps in this list on my emulator but my app is not one of them. Does someone know how to get my app onto the list?
Here is how I am sending the intent:
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_LAUNCHER);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
Here the relevant part of the manifest:
<activity
android:name=".NewHome"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</activity>
If you want to show your application in Default launchers. Change your code as:
<activity
android:name=".NewHome"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
I've written following class to start my application activity Home.class but on device start up it shows error forced close.
public class MyBootRecvr extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, Home.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_FROM_BACKGROUND);
context.startActivity(i);
Toast.makeText(context, "Where is my KeyBoard", Toast.LENGTH_LONG)
.show();
}
}
Permissions and receiver tags in application.
<receiver
android:name=".Home"
android:enabled="true"
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>
If this is the only code in your application, it will never run. Android applications must have at least one subclass of Activity as a starting point for when you run the app. The Activity could should at a minimum look something like this:
class MyActivity extends Activity {
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.my_layout); /* my_layout should be an XML layout in res/layout */
}
}
Make sure the following code in is your AndroidManifest.xml file
<activity android:name="MyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
If you create a new Android project in Eclipse, it will do this setting up for you. There are a lot of tutorials on both setting up a basic application in Android, and using Eclipse to do it for you.
doing this solve the problem
thanks to xono
<receiver
android:name=".MyBootRecvr"
android:enabled="true"
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>