我在项目中添加了READ_PHONE_STATE权限。
方案: 我刚刚打开我的应用程序并获得权限警告“允许我的应用程序制作和管理电话?" 现在我在后台移动我的应用程序,然后点击我的应用程序图标再次打开 结果:“允许我的应用程序拨打和管理电话?" 权限弹出窗口消失。
任何人都可以告诉这个行为吗? 注意:我使用的是launchMode =" singleInstance"当使用launchMode ="标准"一切正常。
分享我的代码:
<?xml version="1.0" encoding="utf-8"?>
<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">
<activity android:name=".MainActivity" android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
public class MainActivity extends AppCompatActivity {
public static final Integer REQUEST_CODE_FOR_PERMISSION = 0x1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
askForPermission(this, Manifest.permission.READ_PHONE_STATE, REQUEST_CODE_FOR_PERMISSION);
}
public static void askForPermission(Activity context, String permission, Integer requestCode) {
if ((Build.VERSION.SDK_INT >= 23) && (context != null) && (TextUtils.isEmpty(permission) == false)) {
ActivityCompat.requestPermissions(context, new String[]{permission}, requestCode);
}
}}
答案 0 :(得分:1)
基于singleTask行为
如果活动的实例已经存在于单独的任务中,则系统通过调用其onNewIntent()方法将意图路由到现有实例,而不是创建新实例。 < / p>
https://developer.android.com/guide/components/activities/tasks-and-back-stack#TaskLaunchModes
我能够通过处理
中的代码来解决此问题onNewIntent(意图)
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
askForPermission(this, Manifest.permission.READ_PHONE_STATE, REQUEST_CODE_FOR_PERMISSION);
}
答案 1 :(得分:0)
试试这个,
public class MainActivity extends AppCompatActivity {
public static final Integer REQUEST_CODE_FOR_PERMISSION = 0x1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@override
private void onResume(){
askForPermission(this, Manifest.permission.READ_PHONE_STATE, REQUEST_CODE_FOR_PERMISSION);
}
public static void askForPermission(Activity context, String permission, Integer requestCode) {
if ((Build.VERSION.SDK_INT >= 23) && (context != null) && (TextUtils.isEmpty(permission) == false)) {
ActivityCompat.requestPermissions(context, new String[]{permission}, requestCode);
}
}}
答案 2 :(得分:0)
在finish()
onBackPressed()
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
这将再次启动活动,您的代码将在onCreate()
中运行答案 3 :(得分:0)
使用像这样的运行时权限:
public class MainActivity extends Activity {
private static final int PERMISSION_REQUEST_CODE = 200;
Context context;
Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
activity = this;
setContentView(R.layout.activity_splash);
if (!checkPermission()) {
OpenActivity();
} else {
if (checkPermission()) {
requestPermissionAndContinue();
} else {
OpenActivity();
}
}
}
private boolean checkPermission() {
return ContextCompat.checkSelfPermission(this, READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED;
}
private void requestPermissionAndContinue() {
if (ContextCompat.checkSelfPermission(this, READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, READ_PHONE_STATE)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("Permission necessary");
alertBuilder.setMessage("STORAGE permission is necessary to write event!!!");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(SplashActivityANother.this, new String[]{READ_PHONE_STATE,
}, PERMISSION_REQUEST_CODE);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
Log.e("", "permission denied, show dialog");
} else {
ActivityCompat.requestPermissions(this, new String[]{READ_PHONE_STATE
}, PERMISSION_REQUEST_CODE);
}
} else {
OpenActivity();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE) {
if (permissions.length > 0 && grantResults.length > 0) {
boolean flag = true;
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
flag = false;
}
}
if (flag) {
OpenActivity();
} else {
finish();
}
} else {
finish();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void OpenActivity() {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);
}
}
并添加
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
进入menifist档案。
希望这有助于你