How do I start an Activity from a Service? - android

So I've been searching around for a week or so, and it appears to be quite possible to start an Activity from a Service, the general order of things going like this:
Intent myIntent = new Intent();
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
myIntent.setComponent(new ComponentName("[package]", "[class]"));
getApplication().startActivity(myIntent);
When trying this from a WallpaperService, I'm getting what appears to be a wallpaper-specific error requires android.permission.BIND_WALLPAPER. Stack trace shows the startActivity as the culprit.
I have no idea how to give it this permission; I have tried putting android:permission="android.permission.BIND_WALLPAPER" on every tag in the manifest that'll take it, as well as <uses-permission android:name="android.permission.BIND_WALLPAPER" />
Notably, the debug view does have the warning:
WARN/PackageManager(59): Not granting permission
android.permission.BIND_WALLPAPER to package (protectionLevel=3
flags=0xbe46)
General question being asked is in the title.
Thanks in advance!

You want to put android:permission="android.permission.BIND_WALLPAPER" inside only the service tag and nowhere else.

Related

How to find out the reason why Activity doesn't start

I got log cat message from startAnotherActivity() method
private void startAnotherActivity() {
Log.i(TAG, "Entered startAnotherActivity()");
Intent intent = new Intent();
intent.setAction(ANOTHER_ACTIVITY);
intent.addCategory("android.intent.category.DEFAULT");
startActivity(intent);
}
Another activity doesn't start, no other messages in log cat.
How can I resolve this issue?
UPDATE#1:
Sorry, I forgot to mention that AnotherActivity is an Activity in the other application, and therefore ANOTHER_ACTIVITY == 'some.other.app.domain.ANOTHER_ACTIVITY'
Shouldn't Dalvik complain if it cannot find specified activity?
One possible reason may be not declaring other activity in the manifest. You can do this like the following:
<activity android:name="your.package.your.activity">
</activity>
And then you can start the activity by doing the following:
Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
startActivity(intent);
Hope this helps.
Since it's an activity in another application, you may need to set the component (fully qualified package name and fully qualified activity name).
See here: How to start activity in another application?
Or here:
Launch an application from another application on Android
Finally I found out my mistake.
In the project there are two similar messages in two activities, so I thought that runs one, but that was another.
Thank you for your assistance!

Android Device Admin dialogue not showing up - Auto declined

I'm trying to implement locking the screen with my app.
I've worked my way through google documentation and studied the sample.
When i call this
void getAdmin(){
Intent activateDeviceAdminIntent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
activateDeviceAdminIntent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mPolicy.getPolicyAdmin());
activateDeviceAdminIntent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
getResources().getString(R.string.AdminReceiverDescr));
startActivityForResult(activateDeviceAdminIntent, REQ_ACTIVATE_DEVICE_ADMIN);
}
The dialogue doesn't show up (maybe it flashes a little, can't say for sure), it just jumps in my onResult routine and result is "not granted". But my app afterwards shows up in the list in settings/security/device admins and if i enable admin rights manually it locks my screen like a charm.
I've added the permission
uses-permission android:name="android.permission.BIND_DEVICE_ADMIN"
as well as
receiver ... android:permission="android.permission.BIND_DEVICE_ADMIN"
to my manifest.
Does anyone have a clue what i may have missed?
Thx in advance!
Found the problem in my manifest: you have to specify the receiver subclass with a $ sign.
Example:
android:name="com.exampl.PolicyClass$PolicyReceiver"

Android Explicit Intent, Loading second Activities layout, not reaching OnCreate method

This is my first android app attempt after reading "Android 2 Application Development" and lots of stuff online.
Here is the relevant code:
from MovieRatingsActivity.java [my main]
Intent i = new Intent(MovieRatingsActivity.this, DisplayMovies.class);
startActivity(i);
from Manifest:
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayMovies"
android:label="Display Movies" >
</activity>
note: i do not have any intent filters for the second activity. Do I need any if it is an explicit intent that I never plan on interacting with another application? I have tried with multiple combinations of different intent filters just out of spite, but its hard to have this answered, as every source I go to jumps to implicit intents and doesn't answer this question.
As for behavior:
Whether in debug mode, or run mode, when I click on the button and create the intent, the emulator switches to the second activity and displays the label at the top, but nothing else. Worse, in debugger mode, when I try to step-into startActivity(i), it just suspends the main thread and goes no where. Do you need a special debug technique for when jumping to next activity?
There is a chance that my intents are fine, my logic to display the list is wrong, but even still I would like to be able to reach the code in the debugger. I also added a System.out.printline at the beginning of the second activities OnCreate method that is not executing.
Do I need any if it is an explicit intent that I never plan on
interacting with another application?
you dont need any explicit intents in that case.
Do you need a special debug technique for when jumping to next
activity?
You could put a breakpoint in onCreate() of second activity.

How to discover intent names within an existing Android application? (specifically Facebook)

I have a Samsung Galaxy Nexus Android phone and for whatever reason the Facebook app's menu UI is missing. This means I cannot get to the Settings screen. I figure I can start the intent from my own app, but how do I find the intent name? I've looked in the APK, but the manifest appears to be compiled into a binary format.
Does anyone know the intent name for the Facebook settings activity?
How can I get a list of the intents in an APK?
----------- Update -----------
The Facebook app's manifest includes:
<manifest android:versionCode="4130"
android:versionName="1.6.3"
package="com.facebook.katana"
xmlns:android="http://schemas.android.com/apk/res/android">
<activity android:label="#string/home_settings"
android:name=".SettingsActivity" />
I've got this in my code:
public void goToFacebookSettings(Context context) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.facebook.katana",
"com.facebook.katana.SettingsActivity");
context.startActivity(intent);
}
But this fails with:
FATAL EXCEPTION: main
java.lang.SecurityException: Permission Denial: starting Intent
{ act=android.intent.action.VIEW cmp=com.facebook.katana/.SettingsActivity }
Is this an inherent limitation, or can I comply with the security requirements in some way?
<activity android:label="#string/home_settings"
android:name=".SettingsActivity" />
This activity doesn't declare any intent-filters. It also doesn't have the android:exported attribute set to true. Which means it can only be launched by the Facebook app or an app that has the same user ID since it's considered for app-internal use only.
See the android:exported attribute documentation for more information.
Since this is for your personal use, you might be able to edit that attribute into the manifest and rebuild the application (also using apktool).
AFAIR you can use explicit activity invocation only inside of your apk - no way to call externally something that is not advertised.

Launching external application from my app

I would like to launch an app the user selects from within my application. However, I'm not sure how I'd go about doing this. I've tried this:
Intent intent = new Intent();
intent.setAction(Contacts.Intents.SHOW_OR_CREATE_CONTACT);
startActivity(intent);
But this seems to throw an error and force close my application. I also tried adding:
<action android:name="Contacts.Intents.SHOW_OR_CREATE_CONTACT"/>
in the AndroidManifest file, but to no avail.
A look at Logcat shows that it's an "IOexception - no such file or directory". A couple of questions arise from this. I read through the Android docs and noticed that the Contact.Intents class is deprecated. However, it's successor, ContactContracts is aimed at API level 5 whereas I'm targeting API level 3. Could this be the problem? Also, I've hardcoded this application into the code. Is there a way to retrieve the intents of any application the user selects so that they can be launched?
You need to pass extra information into the intent to tell Android what you want to show or create. Otherwise Android doesn't know what activity to start and (presumably in your case) throws an ActivityNotFoundException.
For a contact, you use the generic Intent.ACTION_INSERT_OR_EDIT then use the MIME type of an individual contact (Contacts.People.CONTENT_ITEM_TYPE).
For example:
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(People.CONTENT_ITEM_TYPE);
intent.putExtra(Contacts.Intents.Insert.PHONE, "+1234567890");
intent.putExtra(Contacts.Intents.Insert.PHONE_TYPE, Contacts.PhonesColumns.TYPE_MOBILE);
That will bring up the contacts app, prompting you to select an existing contact to add the phone number to, or to create a new contact.
You don't need to add anything special to your manifest to start external activities. Only if you were to directly manipulate the contacts ContentProvider would you need to add the appropriate CONTACT permissions to your manifest.
I use this code for that purpose:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.settings", "com.android.settings.Settings");
startActivity(intent);
This will launch the Settings app, you can use these also:
intent.setClassName("com.android.music", "com.android.music.MediaPlaybackActivityStarter");
intent.setClassName("com.android.contacts", "com.android.contacts.DialtactsContactsEntryActivity");
intent.setClassName("com.android.contacts", "com.android.contacts.DialtactsActivity");
The first starts the default music app, the second the contacts, and the third the dialer.
Hope this helps.
You need to pass in valid arguments to the apps you start. A lot of apps expect the data URI and / or certain extras to be valid.
Please try the following code:
Intent intent = new Intent(Contacts.Intents.SHOW_OR_CREATE_CONTACT);
this.startActivity(intent);
(sorry if there is something wrong on the syntax, I dont have android in this computer)
And remove the action from the manifest. that is not needed.
The action method is used for something else.
For more info, please look at the android site: http://developer.android.com/reference/android/content/Intent.html
Daniel
The activity you are calling should appear not only in the Manifest for its own package, but in the Manifest for the CALLING package, too.

Categories

Resources