Lab - Intents
启动程序
private void startExplicitActivation() { Log.i(TAG,"Entered startExplicitActivation()"); // TODO - Create a new intent to launch the ExplicitlyLoadedActivity class Intent intent=new Intent(ActivityLoaderActivity.this, ExplicitlyLoadedActivity.class); startActivityForResult(intent, 0); // TODO - Start an Activity using that intent and the request code defined above }
private void startImplicitActivation() { Log.i(TAG, "Entered startImplicitActivation()"); // TODO - Create a base intent for viewing a URL // (HINT: second parameter uses parse() from the Uri class) Uri webpage = Uri.parse("http://www.google.com"); Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage); // TODO - Create a chooser intent, for choosing which Activity // will carry out the baseIntent. Store the Intent in the // chooserIntent variable below. HINT: using the Intent class‘ // createChooser Intent chooserIntent = Intent.createChooser(webIntent, "CHOOSER"); Log.i(TAG,"Chooser Intent Action:" + chooserIntent.getAction()); // TODO - Start the chooser Activity, using the chooser intent startActivity(chooserIntent); }
关联程序
<activity android:name=".MyBrowserActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="http" /> </intent-filter> <!-- TODO - Add necessary intent filter information so that this Activity will accept Intents with the action "android.intent.action.VIEW" and with an "http" schemed URL --> </activity>
Lab - Permissions
读取书签--使用权限
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
private void loadBookmarks() { Log.i(TAG, "Entered loadBookmarks()"); String text = ""; Cursor query = getContentResolver().query(Browser.BOOKMARKS_URI, projection, null, null, null); query.moveToFirst(); while (query.moveToNext()) { text += query.getString(query .getColumnIndex(Browser.BookmarkColumns.TITLE)); text += "\n"; text += query.getString(query .getColumnIndex(Browser.BookmarkColumns.URL)); text += "\n\n"; } TextView box = (TextView) findViewById(R.id.text); box.setText(text); Log.i(TAG, "Bookmarks loaded"); }
自定义权限
<permission android:name="course.labs.permissions.DANGEROUS_ACTIVITY_PERM" android:protectionLevel="dangerous"></permission> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <!-- TODO - enforce the custom permission on this Activity --> <activity android:name=".DangerousActivity" android:label="@string/app_name" > <!-- TODO - add additional intent filter info so that this Activity will respond to an Implicit Intent with the action "course.labs.permissions.DANGEROUS_ACTIVITY" --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <action android:name="course.labs.permissions.DANGEROUS_ACTIVITY"/> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
Programming Mobile Applications for Android Handheld Systems by Dr. Adam Porter