2017-03-08 16 views
1

作成したIDをアンドロイドデバイスに保存する必要があります。このIDは、インストールされているいくつかのGoogle Appsの最初のもので作成する必要があります。デバイス内にある他のアプリで読み取る必要があります。また、すべてのAndroid搭載端末で動作する必要があります。アンドロイド - どのように文字列を保存し、どのAppからでもアクセスできます。

私はコンテンツプロバイダを使用しようとしましたが、同じ権限を持つ複数のコンテンツプロバイダを持つことはできませんし、各アプリケーションのすべての権限を検索するコンテンツプロバイダごとに異なる権限はありません。

私もユーザー辞書を変更しようとしましたが、一部のアンドロイドバージョン、特にサムスンの場合は、辞書を設定できない、またはデフォルトでインストールされている辞書がインストールされていないため、そのアクセス権がありません。

だから私の質問は次のようになります。

はどのようにして、デバイスにインストールされているすべてのアプリケーションによって作成され、読み取ることができ、グローバル変数、文字列を持つことができますか?これを達成する方法はありますか?何らかのグローバル辞書やそのようなもの?あなたがフォローする最善のアプローチを探しているなら、私が思う

答えて

1

最善のアプローチ

ContentProviders

さらに多くのデータがある場合でも、このオプションを使用すると、アプリケーション間でデータを共有するのに適しています。

Alernativeアプローチ

のApp 1(EX用:APP1パッケージ名が "com.app1" です)。

SharedPreferences prefs = getSharedPreferences("pref1", 
        Context.MODE_PRIVATE); 
      SharedPreferences.Editor editor = prefs.edit(); 
      editor.putString("stringDemo1", strShareValue); 
      editor.commit(); 

App2が(アプリケーション1では共有環境設定からデータをフェッチ)(あなたはウルの要件など、他の30のアプリケーションのためにそれを使用することができます)。 APP1とAPP2の両方で

try { 
      con = createPackageContext("com.app1", 0);//first app package name is "com.sharedpref1" 
      SharedPreferences pref = con.getSharedPreferences(
         "pref1", Context.MODE_PRIVATE); 
      String your_data = pref.getString("stringDemo1", "No Value"); 
     } 
    catch (NameNotFoundException e) { 
       Log.e("Not data shared between two applications", e.toString()); 
     } 

アプリ点で最大30個のマニフェストファイル&ラベル、

android:sharedUserId="any string" 
android:sharedUserLabel="@string/any_string" 

が...同じであり、string.xmlをから共有ユーザーラベル必見の両方に同じ共有ユーザーIDを追加

以下の例をよく確認してください。

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.xxxx" 
android:versionCode="1" 
android:versionName="1.0" 
android:sharedUserId="any string" 
android:sharedUserLabel="@string/any_string"> 
+0

私はこれが動作しないことを前に述べました。私は約30のアプリをこのプロバイダを作成する能力を持っている必要があります。もし私がそれらすべてに同じ権限を追加しても、同じ権限で新しいアプリをインストールすることはできません。検索ごとに30以上の権限が必要です。 –

+0

chck update Answer –

+0

あなたの問題を解決した場合は受け入れます。 –

0

私はthis postから一意の識別子を作成し、電話は工場resetedない間に同じになりますAndroidのIDを使用して、これに異なるアプローチを得るために管理している、私はユニークなを持つことができますIDは変更できませんので、どのアプリでもこのIDを読み込むだけです。

これは私が使用するコードです:

/** 
    * Return pseudo unique ID 
    * @return ID 
    */ 
    public static String getUniquePsuedoID(Context context) { 
     // If all else fails, if the user does have lower than API 9 (lower 
     // than Gingerbread), has reset their device or 'Secure.ANDROID_ID' 
     // returns 'null', then simply the ID returned will be solely based 
     // off their Android device information. This is where the collisions 
     // can happen. 
     // Thanks http://www.pocketmagic.net/?p=1662! 
     // Try not to use DISPLAY, HOST or ID - these items could change. 
     // If there are collisions, there will be overlapping data 
     String android_id = Settings.Secure.getString(context.getContentResolver(), 
       Settings.Secure.ANDROID_ID); 
     String m_szDevIDShort = "35" + (Build.BOARD.length() % 10) + android_id + (Build.BRAND.length() % 10) + (Build.CPU_ABI.length() % 10) + (Build.DEVICE.length() % 10) + (Build.MANUFACTURER.length() % 10) + (Build.MODEL.length() % 10) + (Build.PRODUCT.length() % 10); 

     // Thanks to @Roman SL! 
     // https://stackoverflow.com/a/4789483/950427 
     // Only devices with API >= 9 have android.os.Build.SERIAL 
     // http://developer.android.com/reference/android/os/Build.html#SERIAL 
     // If a user upgrades software or roots their device, there will be a duplicate entry 
     String serial = null; 
     try { 
      serial = android.os.Build.class.getField("SERIAL").get(null).toString(); 

      // Go ahead and return the serial for api => 9 
      return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString(); 
     } catch (Exception exception) { 
      // String needs to be initialized 
      serial = "serial"; // some value 
     } 

     // Thanks @Joe! 
     // https://stackoverflow.com/a/2853253/950427 
     // Finally, combine the values we have found by using the UUID class to create a unique identifier 
     return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString(); 
    } 
関連する問題