gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:36:22 27 4
gpt4 key购买 nike

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

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>

这是我的 list ,如果有人能帮助我,我真的很感激。

最佳答案

您必须将 android:protectionLevel="dangerous" 添加到您的权限声明中,否则它会被自动授予,尽管您调用了 requestPermission( )

<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);

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

@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();
}
}

请注意,通过使用权限系统,您还必须应对这样一个事实,即用户不会被显示权限请求伪装为免责声明,如果他们

[...] turned down the permission request in the past and chose the Don't ask again option in the permission request system dialog [...]

(引自 documentation )。

所以也许简单地向 SharedPreferences 写入一个标志也可以完成这项工作。

关于android - 无法在我的代码中激活我的自定义权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46814980/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com