2011-10-21 6 views
1

まあ、私はAndroid初心者です。私は別のアクティビティ(嗜好アクティビティ)を開始する1つのアクティビティと1つのBoradcastReceiverを持つアプリケーションを作成しようとしています。それらはすべて3つの異なるファイルに入っています。私の質問は、これらのコンポーネントの間でプリファレンスを共有する方法です。ブロードキャスト受信者からのプリファレンス・アクティビティーで設定されたプリファレンスを読み取る方法は?アンドロイドはブロードキャストレシーバーからの設定を取得します

+0

この質問はおそらくhttp://stackoverflow.com/questions/2614719/how-do-i-get-the-sharedpreferences-from-a-preferenceactivity-in-androidの正確な複製です –

答えて

4

私は私が使用したコードはここにある前に、この全く同じ質問をしています

public class BootupReceiver extends BroadcastReceiver { 

private static final boolean BOOTUP_TRUE = true; 
private static final String BOOTUP_KEY = "bootup"; 

@Override 
public void onReceive(Context context, Intent intent) { 

    if(getBootup(context)) { 
     NotificationManager NotifyM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification Notify = new Notification(R.drawable.n, 
       "NSettings Enabled", System.currentTimeMillis()); 

     Notify.flags |= Notification.FLAG_NO_CLEAR; 
     Notify.flags |= Notification.FLAG_ONGOING_EVENT; 

     RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification); 
     Notify.contentView = contentView; 

     Intent notificationIntent = new Intent(context, Toggles.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
     Notify.contentIntent = contentIntent; 

        int HELO_ID = 00000; 

     NotifyM.notify(HELLO_ID, Notify); 
    } 

    Intent serviceIntent = new Intent(); 
    serviceIntent.setAction("com.leozar100.myapp.NotifyService"); 
    context.startService(serviceIntent); 
} 

public static boolean getBootup(Context context){ 
    return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(BOOTUP_KEY, BOOTUP_TRUE); 
} 
} 

私はそれがちょうど放送受信機の仕事に役立つと思うので、私はちょうど1を開始し、何もしません開始したサービス。また、この放送受信機は、そのように私のマニフェストに登録されている:起動時に開始し

<receiver android:name=".BootupReceiver"> 
     <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     <category android:name="android.intent.category.HOME" /> 
     </intent-filter> 
</receiver> 

は私の質問への参照がhere

P.S.を見つけることができますandroid.permission.RECEIVE_BOOT_COMPLETED

許可を必要としますようこそstackoverflowへ

+0

ありがとうLeozar100。あなたの答えはとても役に立ちました! – momi

関連する問題