我一直在尝试从其他应用中打开一个活动。但是不起作用。出现Toast消息缺席,您可以从代码中看到。我要打开的活动来自包com.example.samarth.upbuttondemo.ActivityA。此活动具有意图过滤器。我要打开的活动的XML文件是
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();
}
}
});
}
}
答案 0 :(得分:1)
将此内容写入打开whatsapp,例如:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.whatsapp");
startActivity(launchIntent);
您只需将应用包作为方法getLaunchIntentForPackage("com.yourpackage")
答案 1 :(得分:0)
假设您要从App A的活动x导航到App B的活动y。
因此将其添加到活动x(应用A)中:
//calling an activity using <intent-filter> action name
Intent inent = new Intent("com.hmkcode.android.another.app.ANOTHER_ACTIVITY");
startActivity(inent);
现在在活动y的清单(应用B)中添加此意图过滤器
<intent-filter>
<action android:name="com.hmkcode.android.another.app.ANOTHER_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
更多详细信息和解释,请参见this link