2016-01-21 49 views
0

我有一個活動在另一個訪問sharedpreferences的類中運行一個方法。我想創建一個可以通過點擊按鈕來運行相同方法的小部件。只要應用程序在後臺打開,它就會工作,但如果關閉了應用程序,那麼當按下窗口小部件上的按鈕時,應用程序會崩潰。Android:從另一個類的家庭小部件訪問SharedPreferences

我相當肯定,這是因爲我需要訪問sharedpreferences的上下文,我試圖通過活動和小部件單獨傳遞上下文,但它不工作。

基本上,當方法在另一個類中時,如何從小部件訪問共享首選項?

活動代碼:

public class MainActivity extends AppCompatActivity { 

    protected static Context context; 
    SharedPreferences prefs; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     MainActivity.context = getApplicationContext(); 
    } 

    public void panicFromView(View view){ 
     prefs = PreferenceManager.getDefaultSharedPreferences(context); 
     Methods.panic(prefs); 
    } 
} 

部件代碼:

public class PanicWidget extends AppWidgetProvider { 

    public static final String CLICK_PANIC = "PANIC"; 
    SharedPreferences prefs; 

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.panic_widget); 

     Intent intent = new Intent(context, PanicWidget.class); 
     intent.setAction(CLICK_PANIC); 

     PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); 

     remoteViews.setOnClickPendingIntent(R.id.panicButton, actionPendingIntent); 

     appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); 

     prefs = PreferenceManager.getDefaultSharedPreferences(context); 
    } 

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

     super.onReceive(context, intent); 

     if (CLICK_PANIC.equals(intent.getAction())){ 
      panicFromWidget(prefs); 
     } 
    } 

    public void panicFromWidget(SharedPreferences prefsIn){ 
     Methods.panic(prefsIn); 
    } 
} 

方法:

public class Methods { 

    public static Context getAppContext() { 
     return MainActivity.context; 
    } 

    public static void panic(SharedPreferences prefsIn){ 

     //loops through contacts 
     for (int i = 1; i <= 5; i++) { 
      String contactKey = "contact-sms" + i; 
      String contactNo = getContact(contactKey, prefsIn); 

      //sends sms only if contact number exists 
      if (!contactNo.equals("")) { 
       sendSMS(contactNo); 
      } 
     } 
    } 

    //returns contact number from contact name 
    public static String getContact(String key, SharedPreferences prefsIn){ 
     return prefsIn.getString(key, null); 
    } 

    //sends SMS to contact number 
    public static void sendSMS(String phoneNo){ 
     try{ 
      String msg = "test"; 

      SmsManager smsManager = SmsManager.getDefault(); 
      smsManager.sendTextMessage(phoneNo, null, msg, null, null); 

      Toast.makeText(getAppContext(), "SMS Sent", Toast.LENGTH_SHORT).show(); 
     } catch (Exception ex) {} 
    } 
} 
+0

發佈您如何從窗口小部件和活動傳遞上下文的代碼。 –

+0

顯示您必須訪問的代碼sharedpref plzz –

+0

添加了代碼。我嘗試通過共享首選,而不是工作。 –

回答

0

沒關係,它實際工作。不知何故,我的共享首選已被清除,但我不知道它是拋出一個nullpointerexception。