{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Intents - Intents& Intent Filters   An Intent...

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Intents & Intent Filters   An Intent is a data structure that specifies   An operation to be performed   An event that has occurred   Broadcast by one component   Received by 0 or more components   This lectures focuses on using Intents to represent operations, rather than events   String representing the operation   Examples:   Setting the Intent Action new Intent(Intent.ACTION_VIEW); Intent newInt = new Intent(); newInt.setAction(Intent.ACTION_VIEW);   Data associated with the Intent   Formatted as a Uniform Resource Identifier (URI)   Examples:   Data to view on a map ▪  geo:0,0?q=1600+Pennsylvania+Ave +Washington+DC   Number to dial in the phone dialer ▪  tel:+15555555555   Setting the Intent data new Intent(Intent.ACTION_VIEW, Uri.parse("tel:+15555555555")); Intent newInt = new Intent(Intent.ACTION_VIEW); newInt.setData(Uri.parse("tel:+15555555555"));   Additional information about the components that handle the intent   Examples:   Sets the MIME type of the Intent data   E.g., “image/*”   If unspecified, Android will infer the type   Setting the mime type Intent.setType(String type) Intent.setDataAndType(Uri data, String type)   The component to receive this intent   Setting the component Intent(Context packageContext, Class<?> cls); Intent newInt = new Intent (); newInt.setComponent(ComponentName) ; newInt.setClass(Context, Class));   Additional information associated with Intent   Treated as a map (key ­value pairs)   Setting the Extra attribute   Several forms depending on data types, e.g., ▪  putExtra(String name, String value); ▪  putExtra(String name, float value);   EXTRA_EMAIL: List of email recipients Intent newInt= new Intent(Intent.ACTION_SEND); newInt.putExtra ( android.content.Intent.EXTRA_EMAIL, new String{ [email protected], [email protected], [email protected], [email protected] } );   Specify how Intent should be handled   Examples:   FLAG_ACTIVITY_NO_HISTORY ▪  Don’t put this Activity in the History stack   FLAG_DEBUG_LOG_RESOLUTION ▪  Causes extra logging information to be printed when this Intent is processed Intent newInt= new Intent(Intent.ACTION_SEND); newInt.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);   Intents can be used to activate Activities   startActivity(Intent intent)   startActivityForResult(Intent intent, …)   The target Activity can be   Named explicitly in the Intent, or   Determined implicitly via intent resolution   HelloWorldWithLogin   Users must authenticate before they can view the “Hello, Android” screen   LoginActivity   Accepts username & password   If password correct, starts HelloAndroid Activity public class LoginScreen extends Activity { public void onCreate(Bundle savedInstanceState) { … loginButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (checkPassword(uname.getText(), passwd.getText())){ Intent helloAndroidIntent = new Intent(LoginScreen.this, HelloAndroid.class); startActivity(helloAndroidIntent); } } }); }   When the Activity to be activated is not named, the system attempts to find Activities that match the Intent   Called Intent Resolution   A process for matching Intents with Activities that want to receive them   Intent Filters describe which Intents an Activity can handle   Usually specified in an AndroidManifest.xml file   Intent Resolution only matches   Action   Data (both URI and mime data type)   Category <activity ….> <intent ­filter android:icon="drawable resource” android:label="string resource” android:priority="integer” . . . <action android:name=”actionName" /> … </intent ­filter> … </activity>   android:icon – Icon representing the activity   android:label  ­ User ­readable label for the parent component   android:priority – Priority given to the parent component when handling matching Intents   Causes Android to prefer one activity over another   Higher values represent higher priorities <intent ­filter …> … <data android:host="string" android:mimeType="string" android:path="string" android:pathPattern="string" android:pathPrefix="string" android:port="string" android:scheme="string" /> … </intent ­filter> <intent ­filter …> . . . <category android:name="string" /> … </intent ­filter> <intent ­filter …> <action android:name= "android.intent.action.VIEW" /> <category android:name= "android.intent.category.DEFAULT" /> <category android:name= "android.intent.category.BROWSABLE" /> <data android:scheme="geo”/> <intent ­filter>   Note: to receive implicit intents an Activity must specify an IntentFilter with the category   "android.intent.category.DEFAULT” category public class CheckIntents extends Activity { … public void onCreate(Bundle savedInstanceState) { … checkButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { List<String> acts = CheckIntents.getActivitiesForAction( CheckIntents.this, intentText.getText().toString()) // output results } }); } public static List<String> getActivitiesForAction( Context context,String action) { final PackageManager packageManager = context.getPackageManager(); final Intent intent = new Intent(action); final List<ResolveInfo> list = packageManager.queryIntentActivities(intent, 0); final List<String> acts = new ArrayList<String>(); for (ResolveInfo ri : list) { acts.add(ri.activityInfo.name); } return acts; } …   Show application info   % adb shell dumpsys package   HelloAndroidWithLogin   CheckActivityIntents ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern