zoukankan      html  css  js  c++  java
  • 39、在代码中添加权限(判断)

    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>
  • 相关阅读:
    java ->IO流_打印流
    java ->IO流_序列化流与反序列化流
    java ->properties类
    java ->String、StringBuffer、StringBuilder三者之间的区别
    java-> 利用IO操作与递归实现目录的复制
    java
    java ->IO流_转换流
    java ->IO流_字符流
    java ->IO流_字节流
    死循环
  • 原文地址:https://www.cnblogs.com/androidsj/p/4626415.html
Copyright © 2011-2022 走看看