2014-01-13 21 views
0
之前啓動

我遇到了以下問題: 應用程序需要知道是否至少有一次啓動了另一個已安裝的應用程序。 我想到用第一個應用程序在SD上創建一個文件。第二個應用程序知道第一個應用程序至少已經啓動了一次。 有沒有更好的方法來做到這一點?找出應用程序是否在

回答

0

使用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) { 
     } 
+0

感謝: )(我會upvote它,如果我有足夠的代表) – Eric

+0

高興這個幫助你:) – iGio90

相關問題