强制应用程序从第一个活动重新启动(当权限被拒绝时) [英] Force application to restart from first Activity (when a permission is denied)

本文介绍了强制应用程序从第一个活动重新启动(当权限被拒绝时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,当我们在运行时在Android 6.0中 拒绝 权限并 恢复 该应用时,最近的菜单中,应用进程被终止,应用被 强制重启 .据称,这是为了防止任何安全隐患:

It is known that when we deny permissions at runtime in Android 6.0 and resume the app from the recent menu, the app process is killed and the app is forcibly restarted. This is allegedly in order to prevent any security snafus:

值得注意的是,当恢复运行时, 该应用会从我们保留的上一个Activity重新启动. 操作系统显然会跟踪上一个Activity用户访问过.

It is remarkable that, when resumed, the app restarts from the last Activity we left it on. The OS apparently keeps track of the last Activity the user visited.

不幸的是,这是一个问题,因为它破坏了业务逻辑的流程.不允许用户以这种方式中途访问该应用程序.

Unfortunately, this is a problem, as it breaks the flow of the business logic. The user cannot be allowed to access the app midway in this manner.

我的问题是,是否有一种方法可以强制应用程序从应用程序的第一个Activity重新启动,而不是由用户将其保留在其上?

My question is, is there a way to force the app to restart from the first Activity of the application, instead of the one the user left it on?

是否存在与应用程序重新启动/进程终止/权限切换相关的回调?

Are there any callbacks associated with an application restart / process kill / permissions toggle?

这是一个错误的方法吗?如果是这样,怎么办?正确的方法是什么?

Is this a wrong-headed approach? If so, how? And what would be the correct approach?

这种行为当然是在以下情况下发生的:

1. Android Preview M:活动在授予权限后重新创建

2.. 在从Android M nexus 6的设置启用/禁用权限后,应用程序被杀死

3. Android棉花糖:正在运行的应用程序的权限发生了改变

4.. 在按重置应用程序首选项"后,我的应用程序的所有权限都被撤消了

5.. Android 6权限-在设置"中切换权限并返回到应用时会崩溃

推荐答案

您是这个意思吗?

MainActivity:

MainActivity:

public class MainActivity extends AppCompatActivity {

TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt = (TextView)findViewById(R.id.txt);

    txt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this, ActivityB.class);
            intent.putExtra("from","MainActivity");
            startActivity(intent);
            finish();
        }
    });
}
}

ActivityB:

ActivityB :

    public class ActivityB extends AppCompatActivity {

String intentTxt="";
final int MY_PERMISSIONS_REQUEST_PHONE_CALL = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_b);
    intentTxt = getIntent().getStringExtra("from");
}


@Override
protected void onResume() {
    super.onResume();
    if(intentTxt.equals("MainActivity")) {
        if (ContextCompat.checkSelfPermission(ActivityB.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(ActivityB.this, new String[]{android.Manifest.permission.CALL_PHONE},
                    MY_PERMISSIONS_REQUEST_PHONE_CALL);
        }
    }
        intentTxt = "";
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_PHONE_CALL: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "32434"));
                startActivity(intent);

            } else {

                Intent intent = new Intent(ActivityB.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
            return;
        }
    }
}
}

您可以从ActivityB的onRequestPermissionsResult启动splashActivity/HomeActivity.在ActivityB的onResume中检查权限,以便在首次调用ActivityB以及在恢复时都显示权限对话框.

You can start the splashActivity/HomeActivity from onRequestPermissionsResult of ActivityB.Check for permission in onResume of ActivityB so that the permission dialog appears when the ActivityB is called for the first time and also when resumed.

这篇关于强制应用程序从第一个活动重新启动(当权限被拒绝时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆