标签:
1 package com.example.metrox.codepermission; 2 3 import android.content.Context; 4 import android.content.pm.PackageManager; 5 6 /** 7 * Created by MetroX on 2016/6/22. 8 */ 9 public class Hello { 10 public static final String PERMISSON_SAY_HELLO = "com.example.metrox.codepermission.permission.SAY_HELLO"; 11 public static void SayHello(Context context){ 12 int result = context.checkCallingOrSelfPermission(PERMISSON_SAY_HELLO); 13 if(result != PackageManager.PERMISSION_GRANTED){ 14 throw new SecurityException("执行SayHello方法需要权限: " + PERMISSON_SAY_HELLO); 15 } 16 System.out.println("权限测试成功"); 17 } 18 }
1 package com.example.metrox.codepermission; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 6 public class MainActivity extends AppCompatActivity { 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_main); 12 Hello.SayHello(this); 13 } 14 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.metrox.codepermission"> 4 <permission android:name="com.example.metrox.codepermission.permission.SAY_HELLO"></permission> 5 <uses-permission android:name="com.example.metrox.codepermission.permission.SAY_HELLO"></uses-permission> 6 <application 7 android:allowBackup="true" 8 android:icon="@mipmap/ic_launcher" 9 android:label="@string/app_name" 10 android:supportsRtl="true" 11 android:theme="@style/AppTheme"> 12 <activity android:name=".MainActivity"> 13 <intent-filter> 14 <action android:name="android.intent.action.MAIN" /> 15 16 <category android:name="android.intent.category.LAUNCHER" /> 17 </intent-filter> 18 </activity> 19 </application> 20 21 </manifest>
标签:
原文地址:http://www.cnblogs.com/linhongquan/p/5608306.html