<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">
在MainActivty.java中加入权限限制
package com.ldw.test;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
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.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends Activity implements View.OnClickListener {
private Button btn_send;
private Button btn_dail;
private EditText et_number;
private EditText et_content;
private Button btn_toast1, btn_toast2;
private static final int PERMISSION_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.SEND_SMS)
== PackageManager.PERMISSION_DENIED) {
Log.d(“permission”, “permission denied to SEND_SMS - requesting it”);
String[] permissions = {Manifest.permission.SEND_SMS};
requestPermissions(permissions, PERMISSION_REQUEST_CODE);
}
if (checkSelfPermission(Manifest.permission.CALL_PHONE)
== PackageManager.PERMISSION_DENIED) {
Log.d(“permission”, “permission denied to CALL_PHONE - requesting it”);
String[] permissions = {Manifest.permission.CALL_PHONE};
requestPermissions(permissions, PERMISSION_REQUEST_CODE);
}
}
btn_send = (Button) fi
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
ndViewById(R.id.btn_send);
//第一种点击事件的方法
btn_send.setOnClickListener(new MyListener());
//第三种点击事件的方法
show3();
}
//第一种点击事件的方法
class MyListener implements View.OnClickListener{
@Override
public void onClick(View v){
et_content = (EditText) findViewById(R.id.et_content);
et_number = (EditText) findViewById(R.id.et_number);
String content = et_content.getText().toString();
String number = et_number.getText().toString();
System.out.println("===="+number);
System.out.println("===="+content);
SmsManager sm = SmsManager.getDefault();
ArrayList smss = sm.divideMessage(content);
for (String string:smss){
sm.sendTextMessage(number, null, string, null, null);
}
}
}
//第二种点击事件,直接在布局文件中添加android:onClick=“dail”
public void dail(View v){
System.out.println("==============");
et_number = (EditText) findViewById(R.id.et_number);
String number = et_number.getText().toString();
Intent intent = new Intent();
intent.setAction(intent.ACTION_CALL);
intent.setData(Uri.parse(“tel:” + number));
startActivity(intent);
}
//第三种点击事件的方法
public void show3(){
btn_toast1 = (Button) findViewById(R.id.btn_toast1);
btn_toast2 = (Button) findViewById(R.id.btn_toast2);
btn_toast1.setOnClickListener(this);
btn_toast2.setOnClickListener(this);
}
//第三种点击的方法
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.btn_toast1:
Toast.makeText(this, “弹窗1”, Toast.LENGTH_SHORT).show();
break;
case R.id.btn_toast2:
Toast.makeText(this, “弹窗2”, Toast.LENGTH_SHORT).show();
break;
}
}
}
申请contentprovider的相关权限
报错如下:
Caused by: java.lang.SecurityException: Permission Denial: opening provider
com.android.providers.contacts.ContactsProvider2 from ProcessRecord{8e31224
29224:activity.gudd.com.review/u0a212} (pid=29224, uid=10212)requires
android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS