Android的权限机制、通知

小记

权限机制

应用场景:
在我们平常使用的APP中,会出现权限申请的情况,比如说微信要获取电话本信息,获取发送短信的权限等

我下面写了一个例子,走一下权限查询以及获取的流程
xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.runtimepermissiontest">
    <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

java:

package com.example.runtimepermissiontest;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "1";
    Button btn_test_permission;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_test_permission=findViewById(R.id.btn_test_permission);
        btn_test_permission.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_test_permission:
                Log.d(TAG, "onClick: 按钮点击");
                if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE)!= PackageManager.PERMISSION_GRANTED){
                    //未授权,申请权限操作
                    ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CALL_PHONE},1);
                }else {
                    call();
                }
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 1:
                if (grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    call();
                }else {
                    Toast.makeText(this,"拒绝添加权限",Toast.LENGTH_LONG).show();
                }
                break;
        }
    }

    private void call() {
        Intent intent=new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:10086"));
        startActivity(intent);
    }
}

效果:
我是在模拟器上跑的,它不会提示让我去选择是否赋权限,而是直接就附上权限了,了解了一下,原因是这样的:
Android 6以前的版本不会提示赋权限,而是通过xml自动就给加上了

通知

应用场景:
通知的应用场景就很多了,最直接的就是qq、微信的消息提醒
下面写一个例子:

package com.example.contentresolvertest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    Button btn_test_notify;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_test_notify=findViewById(R.id.btn_test_notify);
        btn_test_notify.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_test_notify:
                /*
                *   创建通知的步骤:
                *   1、创建NotificationManager
                *   2、创建Notification
                *   3、显示
                * */
                Intent intent=new Intent(this,PendingIntentShow.class);
                PendingIntent pi=PendingIntent.getActivity(this,0,intent,0);
                NotificationManager manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                Notification notification=new NotificationCompat.Builder(MainActivity.this)
                        .setContentTitle("标题")
                        .setContentText("内容内容内容内容")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setContentIntent(pi)
                        .build();
                manager.notify(1,notification);
                break;
        }
    }
}

效果:
Android的权限机制、通知_第1张图片
当点击通知时会跳转到PendingIntentShow活动下

你可能感兴趣的:(Android)