关于真机权限的问题
老师这里我已经设置了权限,为什么在真机上还是显示没有权限?
31814-31814/com.hala.getdatafromsystemdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hala.getdatafromsystemdemo, PID: 31814
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.telephony.SmsProvider from ProcessRecord{7e23845 31814:com.hala.getdatafromsystemdemo/u0a157} (pid=31814, uid=10157) requires android.permission.READ_SMS or android.permission.WRITE_SMS
at android.os.Parcel.readException(Parcel.java:1684)
at android.os.Parcel.readException(Parcel.java:1637)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:4278)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:5632)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2278)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1533)
at android.content.ContentResolver.query(ContentResolver.java:518)
at android.content.ContentResolver.query(ContentResolver.java:475)
at com.hala.getdatafromsystemdemo.MainActivity$1.onClick(MainActivity.java:29)
at android.view.View.performClick(View.java:5647)
at android.view.View$PerformClick.run(View.java:22462)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6379)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
正在回答 回答被采纳积分+1
报错
03-19 16:43:42.666 32217-32217/com.hala.getdatafromsystemdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hala.getdatafromsystemdemo, PID: 32217
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.telephony.SmsProvider from ProcessRecord{f807bbe 32217:com.hala.getdatafromsystemdemo/u0a69} (pid=32217, uid=10069) requires android.permission.READ_SMS or android.permission.WRITE_SMS
at android.os.Parcel.readException(Parcel.java:1684)
at android.os.Parcel.readException(Parcel.java:1637)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:4278)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:5632)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2278)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1533)
at android.content.ContentResolver.query(ContentResolver.java:518)
at android.content.ContentResolver.query(ContentResolver.java:475)
at com.hala.getdatafromsystemdemo.MainActivity$1.onClick(MainActivity.java:43)
at android.view.View.performClick(View.java:5647)
at android.view.View$PerformClick.run(View.java:22462)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6379)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
package com.hala.getdatafromsystemdemo; import android.Manifest; import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.database.Cursor; import android.net.Uri; import android.provider.ContactsContract; import android.provider.Settings; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private static final int MY_PERMISSION_REQUEST_CODE =101 ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.sms).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //1。获取内容处理者 ContentResolver resolver = getContentResolver(); //2。查询方法 //content://sms //content://sms/inbox 收件箱 //content://sms/sent 发件箱 //content://sms/draft 草稿箱 Uri uri = Uri.parse("content://sms"); Cursor c = resolver.query(uri, null, null, null, null); //3。解析Cursor //遍历Cursor while(c.moveToNext()) { String msg = ""; //这里用for循环遍历的方法会输出所有信息,比较杂乱 /* //遍历该行的列 for (int i = 0; i < c.getColumnCount(); i++) { //参数:列的索引 msg += c.getString(i) + " "; } */ String address = c.getString(c.getColumnIndex("address")); String body = c.getString(c.getColumnIndex("body")); msg += address + ":" + body; Log.e("TAG", msg); } } }); findViewById(R.id.read).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //1。获取内容处理者 ContentResolver resolver = getContentResolver(); //2。查询方法 Cursor c1=resolver.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null); while (c1.moveToNext()){ String msg=""; /* 对于联系人而言,他们的存储方式是将姓名和其他内容(电话号码)由不同的ContentProvider操作的 姓名与其他内容在不同的表,姓名为主表,其他为从表 主表的主键会在其他表中作为外键来使用 ContactsContract.Contacts.DISPLAY_NAME 姓名 ContactsContract.Contacts._ID 主键 */ String name=c1.getString(c1.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); String _id=c1.getString(c1.getColumnIndex(ContactsContract.Contacts._ID)); Log.e("TAG","姓名是:"+name+",id是:"+_id); String selections=ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?"; Cursor c2=resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI ,null,selections,new String[]{_id},null); while(c2.moveToNext()){ String number=c2.getString(c2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); name+=" "+number; } Log.e("TAG",name); } } }); findViewById(R.id.add).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ContentResolver resolver=getContentResolver(); //1.往一个ContentProvider中插入一条空数据,获取新生成的id //2.利用刚生成的id分别组合姓名和电话号码往另一个ContentProvider中插入数据 ContentValues values=new ContentValues(); Uri uri=resolver.insert(ContactsContract.RawContacts.CONTENT_URI,values); long id= ContentUris.parseId(uri); //插入数据(姓名) values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,"Emilia"); //指定和姓名关联的编号列的内容 values.put(ContactsContract.Data.RAW_CONTACT_ID,id); //指定插入类型 values.put(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE); resolver.insert(ContactsContract.Data.CONTENT_URI,values); //插入电话号码 values.clear(); values.put(ContactsContract.CommonDataKinds.Phone.NUMBER,"13223346728"); values.put(ContactsContract.Data.RAW_CONTACT_ID,id); values.put(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); //指定手机还是固话,这里为手机 values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE); resolver.insert(ContactsContract.Data.CONTENT_URI,values); } }); }
public void click(View view){ boolean isAllGranted=checkPermissionAllGranted( new String[]{ Manifest.permission.READ_SMS, Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS } ); if(isAllGranted){ doBackup(); return; } ActivityCompat.requestPermissions( this, new String[]{ Manifest.permission.READ_SMS, Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS}, MY_PERMISSION_REQUEST_CODE); } private boolean checkPermissionAllGranted(String[] strings) { for(String permission:strings){ if(ContextCompat.checkSelfPermission(this,permission)!= PackageManager.PERMISSION_GRANTED){ return false; } } return true; } public void onRequestPermissionResult(int requestCode,String[] permissions,int[] grantResults){ super.onRequestPermissionsResult(requestCode,permissions,grantResults); if(requestCode==MY_PERMISSION_REQUEST_CODE){ boolean isAllGranted=true; for(int grant:grantResults){ if(grant!=PackageManager.PERMISSION_GRANTED){ isAllGranted=false; break; } } if(isAllGranted){ doBackup(); }else { openAppDetails(); } } } private void openAppDetails() { AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setMessage("备份通讯录需要访问'通讯录'和'外部存储器',请到'应用信息->权限'中授予!"); builder.setPositiveButton("去手动授权", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent=new Intent(); intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setData(Uri.parse("package:"+getPackageName())); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); startActivity(intent); } }); builder.setNegativeButton("取消",null); builder.show(); } private void doBackup() { Toast.makeText(this, "正在备份通讯录...", Toast.LENGTH_SHORT).show(); } }
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星