标签:
1 public class MainActivity extends Activity {
2 @Override
3 protected void onCreate(Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5 setContentView(R.layout.activity_main);
6
7 Hello.sayHello(this);
8 }
9 }
public class Hello {
public static final String PERMISSION_SAY_HELLO = "com.example.authoritydemo.permission.SAY_HELLO";
public static void sayHello(Context cxt) {
int checkResult = cxt.checkCallingOrSelfPermission(PERMISSION_SAY_HELLO);
// 判断是否被允许
if (checkResult != PackageManager.PERMISSION_GRANTED) {
throw new SecurityException("执行方法需要com.example.authoritydemo.permission.SAY_HELLO权限");
}
Toast.makeText(cxt, "Say Hello www.baidu.com", Toast.LENGTH_SHORT).show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.authoritydemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<permission android:name="com.example.authoritydemo.permission.SAY_HELLO" />
<uses-permission android:name="com.example.authoritydemo.permission.SAY_HELLO" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
标签:
原文地址:http://www.cnblogs.com/androidsj/p/4626415.html