2011-07-27 16 views
0

我想調用另一個類的方法,但這會導致我的應用崩潰。我的android服務導致應用崩潰

這是我在做什麼在引導運行

public class BootReceiver extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent intent) { 
      MyApp test = new MyApp(); 
      test.run_service(); 
    } 
} 

我註釋掉從run_service所有行()來找出病因是什麼,以及它的這條線

SharedPreferences settings = getApplicationContext().getSharedPreferences(PREFS_NAME, 0); 

如果我從該類中調用它(當應用程序是前景時),它工作正常,那麼爲什麼我不能從onReceieve內部調用它?

更新補充通知代碼:

CharSequence title = "MyApp"; 
CharSequence message = "Hello World"; 

NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
Notification n = new Notification(R.drawable.icon, "Hi!", System.currentTimeMillis()); 

Intent notificationIntent = new Intent(context, MyApp.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
n.setLatestEventInfo(MyApp.this, title, message, pendingIntent); 
nm.notify(NOTIFICATION_ID, n); 

回答

1

嘗試這種方式 調用這樣

test.run_service(context); 

您的run方法

void run_service(Context context){ 
    SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0); 
} 
+0

好是固定的。但是這個功能也使用通知服務。我該如何使用這種情況? – mrburns

+0

問題用代碼 – mrburns

+0

更新,您可以使用上下文對象,如context.getSystemSerive(Context.NOTIFICATION_SERVICE),並在Intent/PendingIntent中使用上下文而不是「this」 – Pratik