Failing to start an activity with an implicit intent - android

I am a little confused why the implicit intent call is failing. When trying to start an intent I keep getting the following error:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://org.chrisolsen.crossfit.providers.WorkoutProvider/workouts typ=vnd.android.cursor.dir/vnd.chrisolsen.crossfit.workout }
AndroidManifest
<activity android:name=".activities.WorkoutsActivity"
android:label="#string/title_workouts" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:mimeType="vnd.android.cursor.dir/vnd.chrisolsen.crossfit.workout"/>
</intent-filter>
</activity>
<provider
android:name=".providers.WorkoutProvider"
android:authorities="org.chrisolsen.crossfit.providers.WorkoutProvider" />
Calling activity (dashboard)
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(WorkoutProvider.CONTENT_URI, "vnd.android.cursor.dir/vnd.chrisolsen.crossfit.workout");
startActivity(intent);
Called activity (workouts). It doesn't make it here
Uri uri = getIntent().getData();
...
It seems like it should be simple, but I am confused to why it says there is no activity found.
Any ideas?

In order to be started with implicit intents, An activity must declare
<category android:name="android.intent.category.DEFAULT" />

Also, make sure you are using startActivity instead of sendBroadcast. There is a difference between these methods. A broadcast will not be received by an activity's intent filter. You must use a BroadcastReceiver for that.
Note that, although the Intent class is used for sending and receiving these broadcasts, the Intent broadcast mechanism here is completely separate from Intents that are used to start Activities with Context.startActivity(). There is no way for a BroadcastReceiver to see or capture Intents used with startActivity(); likewise, when you broadcast an Intent, you will never find or start an Activity. These two operations are semantically very different: starting an Activity with an Intent is a foreground operation that modifies what the user is currently interacting with; broadcasting an Intent is a background operation that the user is not normally aware of.
Source: http://developer.android.com/reference/android/content/BroadcastReceiver.html
Android docs: sendBroadcast
Android docs: startActivity

Related

Unable to start intent service via IMPLICIT intent

AndroidManifest.xml
<application android:name=".MyApplication"
android:icon="#drawable/icon"
android:label="#string/app_name"
>
<service android:name=".MyService"
android:exported="true">
<intent-filter>
<action android:name="android.service.myapp.MyService.actionA"/>
<action android:name="android.service.myapp.MyService.actionB"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
</application>
If I use the following code, my service is launched:
Intent intent = new Intent(context, MyService.class);
intent.setAction("android.service.myapp.MyService.actionA");
context.startService(intent);
But my service is not started if I launch it with this code:
Intent intent = new Intent("android.service.myapp.MyService.actionA");
context.startService(intent);
It is insecure to use an "implicit" Intent to start or bind with a Service. Starting with Lollipop, bindService() requires an explicit Intent (your first example where you specify the Context and Class for the Service.) The behavior of startService() is undefined for implicit Intents used to start a service. From the documentation on startService():
The Intent should contain either contain the complete class name of a specific service implementation to start or a specific package name to target. If the Intent is less specified, it log a warning about this and which of the multiple matching services it finds and uses will be undefined.
If you use the explicit form, you can completely remove the <intent-filter> from the manifest: it is not needed. If you need to specify some type of work to be done by the service via the Intent, consider using a extra within the Intent.

How to open Activity via intent-filter and what is the use of intent-filter?

I'm working with activities I know how to open activities via intent, but I want to know how can I open activity via intent-filter and what is the role of intent-filter to open activities.
How many ways to open the activity?
Activity can even be launched via IntentFilter
try this out
Basically when you install your app, Android system will register the activity with corresponding action, when you declare your activity with custom action, Android system stores the activity with the respective activity. When you launch the intent with your custom action. The system will find the receiving activity and launch it it there is only one activity matching it, if there are more than one Activity receiving that action, System will ask the user to choose which activity to complete the action.
declare activity in manifest as
<activity
android:name=".YourActivity" >
<intent-filter>
<action android:name="your.custom.ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
then you can start this activity by just calling
startActivity(new Intent("your.custom.ACTION"));
An IntentFilter is used with BroadcastReceivers.
The BroadcastReceiver then gets activated when any intent that fits through the filter arrives in the system.
This is generally used to send messages between activities, between different applications, or from the server to the application.
See BroadcastReceiver documentation:
http://developer.android.com/reference/android/content/BroadcastReceiver.html
or this tutorial:
http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html

Android service not starting with a warninig

I have a service that supposed to upload photos in the background. But when I try to start it, it doesn't start. In the logcat I've noticed that I get a warning Implicit intents with startService are not safe : Intent { .....}. I've already tripled checked that the action string is the same in the manifest and what I'm starting with. My code :manifest :
<service android:name=".photosupload.services.PhtosUploadService"
android:exported="false" android:process=":uploadPhotosServiceProcess">
<intent-filter>
<action android:name="com.yoovi.app.photosupload.services.action.START_UPLOAD" />
</intent-filter>
</service>
starting service code :
Intent i = new Intent(PhtosUploadService.ACTION_START_UPLOAD);
i.putExtra(PhtosUploadService.ALBUM_ID, albumID);
context.startService(i);
You need to understand implicit and explicit intents.
Explicit intent means you need to specify the exact class from which the intent will be serviced.
Your code should be similar to
Intent i = new Intent();
i.setClass(this, photosupload.services.PhtosUploadService.class);
If you want to send implicit intent to a service - Please Change to android:exported="true" in manifest.

Difference between implicit and explicit intents [duplicate]

This question already has answers here:
What is the different between Explicit and implicit activity call in android?
(7 answers)
Closed 9 years ago.
I'm confused about the difference between implicit and explicit intents. What is the purpose of implicit and explicit intents, and why are these concepts used?
I am new to Android applications, so please provide some examples.
Implicit activity call
With an intent filter you create action for your activity so other apps can call your activity via an action:
<activity android:name=".BrowserActivitiy" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http"/>
</intent-filter>
</activity>
.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
startActivity(intent);
Explicit activity call
You make a call that indicates exactly which activity class to use:
Intent intent = new Intent(this, ActivityABC.class);
startActivity(intent);
Here's an additional reference
Explicit Intent: Explicit intent names the component.
Implicit Intent: Implicit Intents have not specified a component.
E.g: The java class which should be called Implicit intent asked the system to perform a service without telling the system which java class should do this service.

Why my activity has not started up?

I am using implicit way to start an Activity. But when it runs, LogCat shows me can not find the Activity.
here is my declare of <intent-filter>:
<activity android:name=".GroupEditActivity">
<intent-filter>
<action android:name="android.intent.action.EDIT"></action>
<data mimeType="vnd.android.cursor.item/group"></data>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</activity>
Here is the code to start Activity:
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType(Groups.CONTENT_ITEM_TYPE);
startActivity(intent);
Reference said if I set the type and action the same as Intent-filter, them my Activity can start up. But when I only set action in Intent, it still can start the Activity which Intent-filter contains mimetype. So I totally lost.
here is what i find in official website:
Thanks
logcat details:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.EDITtyp=vnd.android.cursor.item/group }
And I can sure the type and action in java and xml are the same.
I solve the problem myself. The error is cause by <data mimeType. I forgot to add android here. So if change to <data android:mimeType, then everything works fine. But why it doesn't report a exception before, does mimeType is also legal?
Are you launching an activity separate from the main activity? In this case you need to call the name of that activity. If you're trying to launch your main activity, it doesn't need to call an intent.
Intent intent = new Intent(this, MyOtherActivity.class);
startActivity(intent);

Categories

Resources