我想用权限做一个简单的练习。我需要为我的应用程序定义并强制执行名为“DangerousApp”的自定义权限,之后我需要设置另一个应用程序“PermissionsLab”,以便它可以使用“DangerousApp”,但是当我尝试启动“ DangerousApp“使用”PermissionLab“我收到错误:”不幸的是,PermissionsLab已停止“
PermissionsLab的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="course.labs.permissionslab"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<!-- TODO - add uses-permission elements -->
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
<uses-permission android:name="course.labs.permissions.DANGEROUS_ACTIVITY_PERM"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ActivityLoaderActivity"
android:label="@string/title_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BookmarksActivity"
android:label="@string/title_permissions" >
</activity>
<activity
android:name=".GoToDangerousActivity"
android:label="@string/title_activity_customization" >
</activity>
</application>
</manifest>
DangerousApp的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="course.labs.permissionslab"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<!-- TODO - add uses-permission elements -->
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
<uses-permission android:name="course.labs.permissions.DANGEROUS_ACTIVITY_PERM"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ActivityLoaderActivity"
android:label="@string/title_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BookmarksActivity"
android:label="@string/title_permissions" >
</activity>
<activity
android:name=".GoToDangerousActivity"
android:label="@string/title_activity_customization" >
</activity>
</application>
</manifest>
用于启动DangerousApp的活动:
package course.labs.permissionslab;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class GoToDangerousActivity extends Activity {
private static final String TAG = "Lab-Permissions";
private static final String DANGEROUS_ACTIVITY_ACTION = "course.labs.permissions.DANGEROUS_ACTIVITY";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.go_to_dangerous_activity);
Button startDangerousActivityButton = (Button) findViewById(R.id.start_dangerous_activity_button);
startDangerousActivityButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startDangerousActivity();
}
});
}
private void startDangerousActivity() {
Log.i(TAG, "Entered startDangerousActivity()");
startActivity(new Intent(DANGEROUS_ACTIVITY_ACTION));
}
}
感谢您的澄清。
答案 0 :(得分:1)
权限可以帮助您保护服务,应用程序,活动...... 这里的文件: http://developer.android.com/guide/topics/security/permissions.html#declaring http://developer.android.com/guide/topics/security/permissions.html#manifest
例如,如果您想要使用权限保护活动,请在AndroidManifest中声明权限并将其置于活动中:
<manifest [...] >
<permission android:name="com.mycompany.MY_PERMISSION" android:protectionLevel="normal"
android:description="@string/permission_desc"
android:label="@string/permission_label" />
<application [...] >
<activity [...] android:permission="com.mycompany.MY_PERMISSION">
</activity>
</application>
</manifest>
在另一个应用程序中,如果要调用受权限保护的活动,则必须在AndroidManifest中添加:
<uses-permission android:name="com.mycompany.MY_PERMISSION" />