无法在 android 中授予我的自定义内容提供商权限?
Not able to grant my custom content provider permission in android?
我正在开发一个内容提供商应用程序。
在该应用程序的清单中,我在应用程序标签中放置了一个提供者元素。
以下是代码
<provider
android:name=".PlatesContentProvider"
android:authorities="com.tag.custom_contentproviderdemo.Plates"
android:enabled="true"
android:readPermission="PlatesContentProvider._READ_PERMISSION"
android:writePermission="PlatesContentProvider._WRITE_PERMISSION"
android:exported="true" >
我还开发了另一个应用程序,即 CPClient。它将 read/write 数据发送给上面提到的内容提供者。
我在其清单文件中被声明为元素。
以下是代码
<uses-permission android:name="PlatesContentProvider._READ_PERMISSION"/>
<uses-permission android:name="PlatesContentProvider._WRITE_PERMISSION"/>
在 CPClient 的启动器 activity 中检查了此权限。如果未被授予意味着我将请求权限。
但是在请求权限时它不显示请求权限对话框并且总是不授予权限。
以下是代码
private final String permissionsRequired="PlatesContentProvider._READ_PERMISSION";
private final String permissionsRequired2= "PlatesContentProvider._WRITE_PERMISSION";
private String[] permissions=new String[]{permissionsRequired,permissionsRequired2};
public void process()
{
checkPermission();
if(!permission_granted)
{
Toast.makeText(this,"Permission not granted.Requesing permission....",Toast.LENGTH_SHORT).show();
requestPermission1();return;
}
else
{
bindWidgetEvents(); //After permission granted I will perform some process
Log.d(TAG,"Permission granted");return;
}
}
public void checkPermission()
{
int permit=this.checkSelfPermission(permissionsRequired);
if(permit== PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "permission granted");
permission_granted=true;return;
}
else
Log.d(TAG,"permission not granted");
permission_granted=false;
}
public void requestPermission1()
{
//boolean show=this.shouldShowRequestPermissionRationale(permissionsRequired);
//Log.d(TAG,"need to show = "+show);
Thread thread=new Thread()
{
public void run()
{
Log.d(TAG,"B4 call request permission");
requestPermissions(permissions,CONTENT_PROVIDER_PERMISSION_REQUEST_CODE);
Log.d(TAG,"after call request permission");
}
};
thread.start();
}
public void onRequestPermissionsResult(int requestCode,String permissions1[], int[] grantResults)
{
Log.d(TAG,"onRequestPermissionsResult. req code="+requestCode);
if(requestCode==CONTENT_PROVIDER_PERMISSION_REQUEST_CODE)
{
Log.d(TAG,"Content provider permsion request code");
if(grantResults==null)
{
Log.d(TAG,"grant results is null");
Toast.makeText(this,"UnKnown error happened",Toast.LENGTH_LONG).show();
return;
}
int grant_results_size=grantResults.length;
Log.d(TAG,"Grant result size="+grant_results_size);
if(grant_results_size<1)
{
Log.d(TAG,"grant results size is <1");
Toast.makeText(this,"UnKnown error happened",Toast.LENGTH_LONG).show();
return;
}
if(grantResults[0]!=PackageManager.PERMISSION_GRANTED)
{
Log.d(TAG,"Permission not granted");
Toast.makeText(this,"Read Permission not granted",Toast.LENGTH_LONG).show();
}
if(grantResults[1]!=PackageManager.PERMISSION_GRANTED)
{
Log.d(TAG,"Permission not granted");
Toast.makeText(this,"Write Permission not granted",Toast.LENGTH_LONG).show();return;
}
Log.d(TAG,"Permission granted");
Toast.makeText(this,"Permission granted",Toast.LENGTH_SHORT).show();
bindWidgetEvents();
return;
}
else
{
Log.d(TAG,"not a Content provider permsion request code");
}
}
以下是androidsdk详情
我的目标和最小 sdk 版本在内容提供商应用程序和我的 CPClient 中都是 23。
不知道哪里会出错?
欢迎大家提出自己的想法。
您应该在 'manifest' 块和提供商中声明您的权限。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package">
<permission
android:name="PlatesContentProvider._READ_PERMISSION"
android:protectionLevel="normal" />
<permission
android:name="PlatesContentProvider._WRITE_PERMISSION"
android:protectionLevel="normal" />
<application>
<provider
android:name=".PlatesContentProvider"
android:authorities="com.tag.custom_contentproviderdemo.Plates"
android:enabled="true"
android:readPermission="PlatesContentProvider._READ_PERMISSION"
android:writePermission="PlatesContentProvider._WRITE_PERMISSION"
android:exported="true" >
</application>
</manifest>
我正在开发一个内容提供商应用程序。
在该应用程序的清单中,我在应用程序标签中放置了一个提供者元素。
以下是代码
<provider
android:name=".PlatesContentProvider"
android:authorities="com.tag.custom_contentproviderdemo.Plates"
android:enabled="true"
android:readPermission="PlatesContentProvider._READ_PERMISSION"
android:writePermission="PlatesContentProvider._WRITE_PERMISSION"
android:exported="true" >
我还开发了另一个应用程序,即 CPClient。它将 read/write 数据发送给上面提到的内容提供者。
我在其清单文件中被声明为元素。
以下是代码
<uses-permission android:name="PlatesContentProvider._READ_PERMISSION"/>
<uses-permission android:name="PlatesContentProvider._WRITE_PERMISSION"/>
在 CPClient 的启动器 activity 中检查了此权限。如果未被授予意味着我将请求权限。
但是在请求权限时它不显示请求权限对话框并且总是不授予权限。
以下是代码
private final String permissionsRequired="PlatesContentProvider._READ_PERMISSION";
private final String permissionsRequired2= "PlatesContentProvider._WRITE_PERMISSION";
private String[] permissions=new String[]{permissionsRequired,permissionsRequired2};
public void process()
{
checkPermission();
if(!permission_granted)
{
Toast.makeText(this,"Permission not granted.Requesing permission....",Toast.LENGTH_SHORT).show();
requestPermission1();return;
}
else
{
bindWidgetEvents(); //After permission granted I will perform some process
Log.d(TAG,"Permission granted");return;
}
}
public void checkPermission()
{
int permit=this.checkSelfPermission(permissionsRequired);
if(permit== PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "permission granted");
permission_granted=true;return;
}
else
Log.d(TAG,"permission not granted");
permission_granted=false;
}
public void requestPermission1()
{
//boolean show=this.shouldShowRequestPermissionRationale(permissionsRequired);
//Log.d(TAG,"need to show = "+show);
Thread thread=new Thread()
{
public void run()
{
Log.d(TAG,"B4 call request permission");
requestPermissions(permissions,CONTENT_PROVIDER_PERMISSION_REQUEST_CODE);
Log.d(TAG,"after call request permission");
}
};
thread.start();
}
public void onRequestPermissionsResult(int requestCode,String permissions1[], int[] grantResults)
{
Log.d(TAG,"onRequestPermissionsResult. req code="+requestCode);
if(requestCode==CONTENT_PROVIDER_PERMISSION_REQUEST_CODE)
{
Log.d(TAG,"Content provider permsion request code");
if(grantResults==null)
{
Log.d(TAG,"grant results is null");
Toast.makeText(this,"UnKnown error happened",Toast.LENGTH_LONG).show();
return;
}
int grant_results_size=grantResults.length;
Log.d(TAG,"Grant result size="+grant_results_size);
if(grant_results_size<1)
{
Log.d(TAG,"grant results size is <1");
Toast.makeText(this,"UnKnown error happened",Toast.LENGTH_LONG).show();
return;
}
if(grantResults[0]!=PackageManager.PERMISSION_GRANTED)
{
Log.d(TAG,"Permission not granted");
Toast.makeText(this,"Read Permission not granted",Toast.LENGTH_LONG).show();
}
if(grantResults[1]!=PackageManager.PERMISSION_GRANTED)
{
Log.d(TAG,"Permission not granted");
Toast.makeText(this,"Write Permission not granted",Toast.LENGTH_LONG).show();return;
}
Log.d(TAG,"Permission granted");
Toast.makeText(this,"Permission granted",Toast.LENGTH_SHORT).show();
bindWidgetEvents();
return;
}
else
{
Log.d(TAG,"not a Content provider permsion request code");
}
}
以下是androidsdk详情
我的目标和最小 sdk 版本在内容提供商应用程序和我的 CPClient 中都是 23。
不知道哪里会出错?
欢迎大家提出自己的想法。
您应该在 'manifest' 块和提供商中声明您的权限。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package">
<permission
android:name="PlatesContentProvider._READ_PERMISSION"
android:protectionLevel="normal" />
<permission
android:name="PlatesContentProvider._WRITE_PERMISSION"
android:protectionLevel="normal" />
<application>
<provider
android:name=".PlatesContentProvider"
android:authorities="com.tag.custom_contentproviderdemo.Plates"
android:enabled="true"
android:readPermission="PlatesContentProvider._READ_PERMISSION"
android:writePermission="PlatesContentProvider._WRITE_PERMISSION"
android:exported="true" >
</application>
</manifest>