Skip to content

Instantly share code, notes, and snippets.

@nightbear1009
Last active August 29, 2015 14:13
Show Gist options
  • Save nightbear1009/a74e27156e3006976865 to your computer and use it in GitHub Desktop.
Save nightbear1009/a74e27156e3006976865 to your computer and use it in GitHub Desktop.
startActivity
package startnineyi.com.startnineyi;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isInstallShopApp()){
openApp();
}else{
openApp();
}
}
});
final EditText txt = (EditText)findViewById(R.id.textview);
findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(txt.getText() != null) {
Uri otherShopUri = Uri.parse(txt.getText().toString());
if (otherShopUri == null || otherShopUri.getPath() == null) {
return;
}
Intent intent=getPackageManager().getLaunchIntentForPackage("com.nineyi.shop.s000360");
intent.setData(otherShopUri);
startActivity(intent);
}
}
});
}
private boolean isInstallShopApp() {
PackageInfo packageInfo;
try {
packageInfo = getPackageManager().getPackageInfo("com.nineyi.shop.s000360", PackageManager.GET_ACTIVITIES);
} catch (PackageManager.NameNotFoundException e) {
packageInfo = null;
e.printStackTrace();
}
return packageInfo != null;
}
public void openApp(){
if(isInstallShopApp()){
openShopApp();
}else{
openGooglePlay();
}
}
public void openShopApp(){
Intent intent=getPackageManager().getLaunchIntentForPackage("com.nineyi.shop.s000360");
startActivity(intent);
}
public void openGooglePlay(){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id=com.nineyi.shop.s000360"));
startActivity(intent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment