Implicit Intent with user-specified action - android

I'm experimenting, specifying my own action for use in an implicit intent. In a single package, I define two activities. ActivityTwo is to be called from onClick() in ActivityOne, using an implicit intent with an action "course.labs.activitylab.MY_ACTION". But I haven't been able to make it work.
In strings.xml:
<string name="myfunnystring">course.labs.activitylab.MY_ACTION</string>
In AndroidManifest.xml:
<activity
android:name=".ActivityTwo"
android:label="#string/title_activity_activity_two" >
<intent-filter>
<action android:name="#string/myfunnystring" />
</intent-filter>
</activity>
In onClick() in the OnClickListener() in onCreate() in ActivityOne.java:
Intent intent = new Intent();
intent.setAction(getString(R.string.myfunnystring));
intent.setFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION);
startActivity(intent);
The program crashes in the emulator, and I find this in the logcat window:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=course.labs.activitylab.MY_ACTION flg=0x8 }
What am I doing wrong?

Add the default category to your intent filter.
<intent-filter>
<action android:name="course.labs.activitylab.MY_ACTION" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

Related

android- Calling Activity using customAction

I know there were some questions similar to this... but they didnt clear my issue.
I want to call an activity using setAction but i am getting run time exception..
"No Activity found to handle Intent"...
here is my manifest file
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity">
<intent-filter>
<action android:name="com.example.prem.intents.Main2Activity"/>
</intent-filter>
</activity>
here is my MainActivity code...
b4=(Button)findViewById(R.id.btn3);
b4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String s="com.example.prem.intents.Main2Activity";
Intent i=new Intent();
i.setAction(s);
startActivity(i);
}
});
Could you try code in below in your manifest?
<activity android:name=".Main2Activity">
<intent-filter>
<action android:name="com.example.prem.intents.Main2Activity"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
In Documentation they says:
To receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category. If you do not declare this category in your intent filter, no implicit intents will resolve to your activity.

Can not list app using intent filter in Broadcast Receiver

I want to list apps (with same intent filter). I was able to achieve this by adding intent filter to an activity
<activity
android:name=".Activities.MainActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden|adjustNothing">
<intent-filter>
<action android:name="com.example.identifier" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="isApp" />
</intent-filter>
</activity>
and i could retrieve all apps having this intent with
String uri = "isApp:";
Intent intent = new Intent("com.example.identifier",
Uri.parse(uri));
PackageManager manager = getPackageManager();
List<ResolveInfo> list = manager.queryIntentActivities(intent, 0);
However, this launches the activity when shown in intentChoose using this snippet:
Intent zoneIntent = new Intent("com.example.identifier",
Uri.parse(uri));
Intent openInChooser = Intent.createChooser(zoneIntent, "Complete Action with").setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(openInChooser);
But i would want this to call a broadcast receiver. So, i moved the intent to a broadcast receiver in AndroidManifest.xml like:
<receiver
android:name=".ExampleReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.example.identifier" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="isApp" />
</intent-filter>
</receiver>
and the snippet that returns the number of apps with this intent returns 0 now even when the app is still on this device. Could this be done with Broadcast receiver or should i consider with another approach. Thanks.
Calling queryIntentActivities() will only return Activitys. It won't return BroadcastReceivers. If you want to do this with BroadcastReceivers, then you need to call queryBroadcastReceivers() instead.

Launching custom implicit intent

Two activities are installed having the following manifest files on device respectively:
The First app's activity has in its manifest:-
where,
package="com.example.tictactoe"
<intent-filter>
<action android:name="com.example.tictactoe.YOYO" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
</intent-filter>
The second app's activity has in its manifest:-
where,
package="com.example.project"
<intent-filter>
<action android:name="com.example.project.YOYO" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
</intent-filter>
Now, i want to start one of these activity from third application using the following code:
i=new Intent();
i.setAction("YOYO");
i.putExtra("KEY","HII..i am from third app");
startActivity(i);
But execution shows an error:-
03-11 08:12:30.496: E/AndroidRuntime(1744): FATAL EXCEPTION: main
03-11 08:12:30.496: E/AndroidRuntime(1744): android.content.ActivityNotFoundException:
No Activity found to handle Intent { act=ACTION_SEND (has extras) }
You need to supply the full action name; supply the mimeType you used in manifest by calling setType() in your intent.
Manifest :
<intent-filter>
<action android:name="com.example.tictactoe.YOYO" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
Java :
Intent i=new Intent();
i.setAction("com.example.tictactoe.YOYO");
i.setType("text/plain");
i.putExtra("KEY","HI..i am from third app");
startActivity(i);
You need to supply the full action:
i=new Intent();
i.setAction("com.example.tictactoe.YOYO");
i.putExtra("KEY","HII..i am from third app");
startActivity(i);
Or (depending which project you want to launch):
i.setAction("com.example.project.YOYO");
You can do it also via: (supply action directly in constructor)
i=new Intent("com.example.tictactoe.YOYO");
i.putExtra("KEY","HII..i am from third app");
startActivity(i);
Also loose the data mimeType or read up on how to use it. Because via putExtra is not going to work.
First of all you need to ensure that the name of the intent is the fully qualified name with the package name is the same in the intent filter and the activity firing the intent. In this case: "YOYO" should be "com.example.tictactoe.YOYO". You should also remove the mime type since you are not including data in the setData(), you are in this case using a bundle. So you should have for the activity firing the intent:
ACTIVITY FIRING INTENT
i=new Intent();
i.setAction("com.example.tictactoe.YOYO");
i.putExtra("KEY","HII..i am from third app");
startActivity(i);
and for the receiving activity's entry in the manifest: You need to ensure you set the category as DEFAULT and remove the data type tag.
ACTIVITY RECEIVING INTENT
<intent-filter>
<action android:name="com.example.tictactoe.YOYO" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Intent object not triggering intent-filter

Perhaps I'm misunderstanding how Intents and intent-filters work, but it seems to me that this should be a straight-forward case. However, it's not working.
Here is the Intent I'm sending:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setType("vnd.android.cursor.item/vnd.connectsy.event");
startActivity(i);
And here is the intent-filter:
<activity android:name=".events.EventView">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:mimeType="vnd.android.cursor.item/vnd.connectsy.event" />
</intent-filter>
</activity>
And finally, the error I'm recieving:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW typ=vnd.android.cursor.item/vnd.connectsy.event }
Here's the answer:
Android treats all implicit intents
passed to startActivity() as if they
contained at least one category:
"android.intent.category.DEFAULT" (the
CATEGORY_DEFAULT constant). Therefore,
activities that are willing to receive
implicit intents must include
"android.intent.category.DEFAULT" in
their intent filters.
Add
<category android:name="android.intent.category.DEFAULT" />
to your intent filter in AndroidManifest.xml

Unable to launch an activity using implicit intent specifying only action

I am trying to launch an activity by specifying only its action(custom) defined in its intent filter from an activity in other application. The activity to be launched is the main activity of its application thus have android.intent.action.MAIN & android.intent.category.LAUNCHER set as action and category in its intent filter. Now according to android doc on Intent and Intent Filter, i do not need to specify DEFAULT category at all in this case. But doing the same i am i am unable to launch the activity.
LogCat says, Activity could not be found...
Am i misinterpreting the text or is there something else missing...?
Code, used for calling the activity
Intent i=new Intent();
i.setAction("android.AddressBookV4.firstActivity");
startActivity(i);
Definition of Activity being called in the manifest file
<activity android:name=".AddressBook"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.AddressBookV4.firstActivity" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You need to add the LAUNCHER category to your Intent, since the <intent-filter> says that it matches the following Intents (in pseudocode):
((action IS "android.AddressBookV4.firstActivity")
OR (action IS "android.intent.action.MAIN"))
AND (category IS "android.intent.category.LAUNCHER")

Categories

Resources