gpt4 book ai didi

java - 验证 Intent 是否进行有效的 Activity

转载 作者:行者123 更新时间:2023-12-02 11:05:22 24 4
gpt4 key购买 nike

public void action(View view){
Intent intent = new Intent(Settings.ACTION_DEVICE_INFO_SETTINGS);
if(intent!=null) //here is my problem, it return true always
{startActivity(intent);}
else{k++;
Context context = getApplicationContext();
CharSequence mesajText = "Failed To Open! " + k;
int duration = 3;
Toast screen_message = Toast.makeText(context,mesajText,duration);
screen_message.show();
}
}

如何验证我的“Intent ”是否具有有效的 Activity (打开时有效)或无效的 Activity (打开时应用程序崩溃)?

最佳答案

if(intent!=null) 将始终评估为 true,因为您在 if 条件 之前初始化 Intent 。如果您想检查是否可以处理Intent,请使用resolveActivity,如下所示:

Intent intent = new Intent(Settings.ACTION_DEVICE_INFO_SETTINGS);
PackageManager packageManager = getPackageManager();
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent);
} else{
k++;
CharSequence mesajText = "Failed To Open! " + k;
int duration = 3;
Toast screen_message = Toast.makeText(this,mesajText,duration);
screen_message.show();
}

关于java - 验证 Intent 是否进行有效的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51008260/

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