Reputation:
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();
}
}
How can I verify if my 'intent' have a valid activity(works when it's open) or an invalid one(app crash when it's open)?
Upvotes: 0
Views: 112
Reputation: 24947
if(intent!=null)
will always evaluate to true
because your are initializing intent just before the if condition
. If you want to check if Intent
can be handled then use resolveActivity
as follows:
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();
}
Upvotes: 1