使用sharedpreferences:
在需要啓動的第一個應用程序至少一次(在創建方法)
//This will store a string with a value "yessir" when the app is launched. You can tweak it a bit by adding something like if !launched string exist skip the creation to avoid creating the string at every launch.
SharedPreferences pref = getSharedPreferences("my_prefs", Context.MODE_WORLD_READABLE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("launched", "yessir");
editor.commit();
在第二個應用程序:
try {
Context myContext = createPackageContext("package_name_of_the_first_app", Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences pref = myContext.getSharedPreferences(
"my_prefs", Context.MODE_PRIVATE);
String isItLaunched = pref.getString("launched", "");
if (isItLaunched.equals("yessir") {
// The first app got launched so do something
} else {
// the first app was not launched so do something else
}
} catch (NameNotFoundException e) {
}
感謝: )(我會upvote它,如果我有足夠的代表) – Eric
高興這個幫助你:) – iGio90