Android 无法在我的代码中激活我的自定义权限

Android 无法在我的代码中激活我的自定义权限,android,android-permissions,Android,Android Permissions,我想请用户同意我在应用程序中提供的免责声明,如果他们同意,下次他们使用应用程序时,我们只需重新向他们显示免责声明几秒钟,而不要求单击任何内容并将他们重定向到应用程序。我能想到的最好的方法是,使用自定义权限,但我确实向清单中添加了权限命令,但我不知道如何在代码中实际激活权限: import java.util.jar.Manifest; public class Language extends Activity { Button engBut, fraBut; // publi

我想请用户同意我在应用程序中提供的免责声明,如果他们同意,下次他们使用应用程序时,我们只需重新向他们显示免责声明几秒钟,而不要求单击任何内容并将他们重定向到应用程序。我能想到的最好的方法是,使用自定义权限,但我确实向清单中添加了权限命令,但我不知道如何在代码中实际激活权限:

import java.util.jar.Manifest;

public class Language extends Activity {

    Button engBut, fraBut;
  //  public static int j;


   public  void click (View view){

     //  j=1;

       Intent intent = new Intent(getApplicationContext(),MainActivity.class);
       startActivity(intent);


    }

    public void fraClick (View view){

     //   j=2;
        Intent intent = new Intent(getApplicationContext(),MainActivity.class);
        startActivity(intent);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_language);

        ImageView logo = (ImageView) findViewById(R.id.logo);

        ImageView montfortLogo = (ImageView) findViewById(R.id.montfortLogo);

        montfortLogo.setImageResource(R.drawable.k);

        logo.setImageResource(R.drawable.j);

        engBut = (Button) findViewById(R.id.engBut);

        engBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

             AlertDialog.Builder builder = new AlertDialog.Builder(Language.this);

                builder.setCancelable(true);
                builder.setTitle("DISCLAIMER");
                builder.setMessage(R.string.result_disclaimer);

                builder.setNegativeButton("Get me out of here.", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.cancel();

                    }
                });
                    builder.setPositiveButton("I agree.", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                            Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                            intent.setAction("com.techideas4you.pharamacy.MyAction");
                            intent.addCategory("android.intent.category.DEFAULT");
                            startActivity(intent);

                        }
                    });
                builder.show();
            }
        });




        fraBut = (Button) findViewById(R.id.fraBut);

        Log.i("Locale", String.valueOf(Locale.getDefault()));


    }
}
这是我的第一页代码

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="example.ex"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <uses-permission android:name="com.android.vending.BILLING" />
    <uses-permission 
    android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission 
    android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <permission android:name="testDisclaimer" android:label="DISCLAIMER" 
     android:description="@string/result_disclaimer"/>
       
    <uses-permission android:name="testDisclaimer"/>


    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
        </activity>
        <activity
            android:name=".ActivityQuestion"
            android:icon="@drawable/home"
            android:label="Home"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity
            android:name=".ActivityResult"
            android:icon="@drawable/home"
            android:label="Home"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity android:name=".Language">
            <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
            <intent-filter >
                <action android:name="com.techideas4you.pharamacy.MyAction" 
         />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
         </activity>
         </application>

         </manifest>

   

这是我的清单,如果有人能在这里帮助我,我真的很感激。

您必须在您的权限声明中添加
android:protectionLevel=“danger”
,否则它将被自动授予权限,尽管您调用
requestPermission()
,但用户不会看到对话框

结果(无论用户是否同意)将在
onRequestPermissionsResult()中传输给您

请注意,通过使用权限系统,您还必须处理这样一个事实,即如果用户拒绝,则不会向其显示伪装为免责声明的权限请求

[…]在过去拒绝了权限请求并选择了 “权限请求系统”对话框中的“不再询问”选项[…]

(引自)

因此,也许简单地为
SharedReferences
编写一个标志也可以完成这项工作

<permission android:name="your.package.name.permission.testDisclaimer" android:label="DISCLAIMER"
            android:description="@string/result_disclaimer"
            android:protectionLevel="dangerous"/>

<uses-permission android:name="your.package.name.permission.testDisclaimer"/>
requestPermissions(new String[]{"your.package.name.permission.testDisclaimer"}, 42);
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == 42 && grantResults != null){
        Toast.makeText(this, "Permission  " + permissions[0] + (grantResults[0] == PERMISSION_GRANTED ? "" : " NOT ") + " granted! ", Toast.LENGTH_LONG).show();
    }
}