I have been trying to open an Activity from a different app. But is doesn't work. Toast message appears Absent as u can see from code. The Activity I want to open is from package com.example.samarth.upbuttondemo.ActivityA. This Activity has an intent filter. The XML file for the activity I want to open is
android:parentActivityName="com.example.samarth.upbuttondemo.MainActivity">
<intent-filter>
<action
android:name="com.example.samarth.upbuttondemo.ActivityA" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setComponent(new ComponentName("com.example.samarth",
"com.example.samarth.ActivityA"));
PackageManager packageManager = getPackageManager();
List activities = packageManager.queryIntentActivities(i,
PackageManager.MATCH_DEFAULT_ONLY);
boolean isIntentSafe = activities.size() > 0;
if(isIntentSafe==true)
{
Toast.makeText(MainActivity.this,"Present",Toast.LENGTH_SHORT).show();
startActivity(i);
}
else
{
Toast.makeText(MainActivity.this,"Absent",Toast.LENGTH_SHORT).show();
}
}
});
}
}