setComponentEnabledSetting causes an exception,it may kill current app? - android

i am willing to disable one activity from another activity by following codes,but it causes a problem: kill current app.
the SDK version is 4.0 .
#Override
public void onCreate(Bundle savedInstanceState) {
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(newComponentName(this,
"com.xxx.launcher.desktop.testActivity"),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
is it lack of sth to be set?

You should take a look at http://developer.android.com/reference/android/content/pm/PackageManager.html#DONT_KILL_APP
You have to be a little careful when you use PackageManager.DONT_KILL_APP, as it can lead to undefined behavior. Especially if the activity is trying to disable itself. It should be fine if you're targeting another Activity, but I'm guessing the Activity you're trying to clear is in the
It looks like your call to ComponentName is incorrect.
You will probably want to call new ComponentName(this.getApplicationContext(), testActivity.class). Or you can use new ComponentName("com.xxx.launcher.desktop","com.xxx.launcher.desktop.testActivity"), where the first argument is the package in the AndroidManifest.xml file, and the second being the full path to the ComponentName. Take a look at the following SO question for information on setting up a ComponentName: When to use which constructor for ComponentName in Android? for more information.

I had the same problem as the OP.
I noticed, at the top of the Run window in Android Studio, that it wasn't trying to launch my default launcher activity, SplashActivity, but another launcher activity within my app:
Launching 'app' on samsung SM-J320FN.
$ adb shell am start -n "com.example/com.example.NotMyDefaultLauncherActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
My app exited unceremoniously when it later attempted to disable NotMyDefaultLauncherActivity (via packageManager.setComponentEnabledSetting(...).
The solution for me was to go into Run > Edit Configurations... > Android App > app, where I changed the launch activity from DefaultActivity to (what should have been the default, as it was the first one listed in my manifest) com.example.SplashActivity.

Related

How to update android app with hidden icon

I use this code for hiding icon:
PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName(this, MainActivity.class);
p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
It works fine, but when I try to update my application, I get this error:
Error while executing: am start -n "../..main.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=../.main.MainActivity launchParam=MultiScreenLaunchParams { mDisplayId=0 mFlags=0 } }
Error type 3
Error: Activity class {../..main.MainActivity} does not exist.
Error while Launching activity
How can I hide icon and still available application update (without uninstall/install in a manual way)?
This is a design problem I think. You need to prevent MainActivity getting launched when it is disabled. If you are forced for some reason to start your MainActivty after updating, just enable your MainActivity first. You can set MainActivity enabled state to false in the Manifest and make it enabled whenever you need for example in onCreate method of the application class, in a service or in a broadcast receiver. Disabling MainActivity in the Manifest probably prevent it to be launched by default and might be the thing you need. It is all based on your need of how and when you want your MainActivity to get disabled and enabled again. For a final comment as you probably know an app doesn't need an activity - with action main or not - to get started.
just remove android.intent.category.LAUNCHER from the Activity and there will be no launcher icon - while it still can be launched, due to android.intent.action.MAIN. If you really need a launcher icon, which can be hidden & shown, simply add one proxy Activity, which has no other purpose than providing the launcher and then start the actual Activity... so that one still can start it directly, no matter if that proxy Activity had been disabled, whether or not.

App does not support launch on secondary displays Android

I get this toast message on the emulator and device when trying to launch a new activity from my initial one on a secondary display. From my understanding this is a newer feature with Android O. I've searched thoroughly but could not find this problem anywhere else thus I am here. I understand this can be done with presentations but I need to getting it working with activities. Could someone tell me why this occurs?
Android does support launching an Activity to a secondary display via code (initiating via an adb command not required). Here is an example:
ActivityOptions options = ActivityOptions.makeBasic();
options.setLaunchDisplayId(d.getDisplayId());
Intent secondIntent = new Intent(this, SecondActivity.class);
secondIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(secondIntent, options.toBundle());
If you are seeing a toast message with the error that it is not supported then in your AndroidManifest.xml add the resizeableActivity="true" property to your activity like so:
<activity
android:name=".SecondActivity"
android:label="#string/app_name"
android:resizeableActivity="true"
>
Android O does not support start activity on the second display except using the "am stack start " command, do you use this to launch activity to the second display?
From the android source code, the warning toast is shown when we tried to put a non-resizeable task on a secondary display with config different from global config.
Maybe you can use the "am task resizeable [0|1|2|3]" commands to make the task resizeable.

How can I set my WatchFaceService as the default launch activity in Android Studio

I have a WatchFaceService (WatchFace) and every time I run my application it switches to the SimpleFace and then I have to set mine as the watchFace which ends up to be quite frustrating after many restarts.
To notice this does happen with the new Android Studio 2
I read around S.O. how to set the default activity but that does not do the same job as my WatchFaceService is not an activity but a service.
Also via the UI of Android Studio 2 it cannot be selected.
Is there a way to achieve this ? I think it might be difficult because actually it's not running an app, but setting the watch's Watchface at every run.
Any ideas?
The short answer is that this isn't possible. Your watch face is a Service, after all, so there's no way that it can be the default (launch) Activity for your app. They're completely different component classes.
But you can get close.
What you need to do is create a tiny little shell Activity that contains only the following code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER)
.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
new ComponentName(getPackageName(),
MyWatchFaceService.class.getName()));
startActivity(intent);
finish();
}
...where MyWatchFaceService is the class name of your watch face service (surprise). You'll also need to declare it in your manifest, of course:
<activity android:name=".FaceActivity"
android:enabled="true"
android:exported="true">
</activity>
Finally, configure your Wear module in Android Studio to launch FaceActivity when you run the app. This is under the Run menu, in Edit Configurations.
Having done that, run your app from AS onto the watch, and it'll open the watch face chooser on-device, with your face selected. From there, one tap will start it.
I can't see a way to eliminate that single tap, though.

Remove from launcher BUT keep enabled / startable

i tried coding it, i tried solving it with google and stackoverflow, nothing found :=) so hopefully someone else has a better idea, im not sure if i get everything right:
i have 2 applications: ad (main app) / adfree-pro (just license starts ad app without ads ;P)
so the problem is, i want to have a pro version (with pro icon) in the launcher, which starts the normal-ad app, which is (the normal ad-app) not in the launcher.
i tried removing the ad-app from the launcher (which due to my research should JUST remove it from the launcher)
pkgMgr.setComponentEnabledSetting(new ComponentName(PACKAGE_NAME, PACKAGE_NAME + ".Main"),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
which results to: the icons in the launcher are correct ;) BUT the application can't be found on the phone, launched, started, even not with a launcher pro activity shortcut. it seems to be there (shortcuts can be created) but i crashes with an activity exception when i try to launch it.
02-18 14:38:59.237: ERROR/AndroidRuntime(9941): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {PACKAGE_NAME/PACKAGE_NAME.Main}; have you declared this activity in your AndroidManifest.xml?
which doesnt seem to belong (the error message)
it looks like there has happened more to the application than just simply removed the entry in the launcher.
thanks a lot guys,
every workaround for this situation appreciated :)
best regards :)
You can't have app installed and hide it's launcher icon. The way I'm addressing it with my application which works similar to yours that I don't try to fight icons but instead the app can be launched using ether icon. Obviously you don't have to do in the main (free) app and the code that launches app from your pro icon will look something like the following:
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
// check if main app is installed. If yes then start it
if (appExists()) {
Log.d(TAG, "Started main app from Pro");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("myapp://com.myapp.main"));
startActivity(intent);
finish();
} else {
// display view with link back to Market
onAppNotExists();
}
}
It's up to you to implement appExists() which is probably some sort of license check
Of course, alternatively you can develop your app's common code as library project and then distribute it in 2 flavors without duplicating the code
BUT the application can't be found on the phone, launched, started,
even not with a launcher pro activity shortcut.
Not application, but activity.
So, if your LAUNCHER activity is BaseActivity, you may create something like BaseFakeActivity (don't forget to set it as LAUNCHER in your manifest instead of your BaseActivity) and which only function is to start your BaseActivity and then finish() itself.
Now you may hide your BaseFakeActivity but you'll still be possible to interact with your BaseActivity.
P.S.: Don't forget to test your app's behaviour after doing things this way ;)

how to run Specific activity in android emulator?

i have created 4 activities in eclipse now i want to run activity 1, 2,3 ,4 repectively one by one in emulator for testing.
can any one guide me how can i run those all???
when i press run button it only runs first activity.
any help would be appriciated.
You could try startActivityForResult but you may need to possibly modify your program your applications to handle this.
I would suggest using one of the android sdk tools called am (activity manager).
In the adb shell:
# am start -n package-name/activity-1-name
# am start -n package-name/activity-2-name
# am start -n package-name/activity-3-name
# am start -n package-name/activity-4-name
Go to the AndroidManifest.xml and cut
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
from the Main Activity. Then paste it into the Activity that you want to start.
To Run A specific Activity First
Change the Activity Name In the setContentView within the Main Activity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Your_Activity_Name);
}
public void onClick(View v) {
Intent i;
i = new Intent(this, YourActivity1.class);
startActivity(i);
i = new Intent(this, YourActivity2.class);
startActivity(i);
i = new Intent(this, YourActivity3.class);
startActivity(i);
i = new Intent(this, YourActivity4.class);
startActivity(i);
}
Android SDK includes the JUnit framework for writing unit tests. You can use the package android.test packages to run activities under JUnit. It may be overkill for what you want but eventually you may need this functionality.
References:
http://junit.sourceforge.net/
http://mylifewithandroid.blogspot.com/2008/11/junit-in-android.html
Go to the Android Manifest file under your workspace root and double click on it to open. Go to the AndroidManifest.xml tab and change the name of the first activity to whatever activity you want to launch at run. Also make sure you rename that first activity to the other activity so ADT doesn't throw errors. Basicall, switch their names in the xml file. I had to do it because I wanted to test each activity individually before linking them. Let me know if you have any other question.

Categories

Resources