开发者

My android service causes the app to crash

I'm trying to call a method of another class but this causes my app to crash.

This is what I'm doing to run it on boot

public c开发者_高级运维lass BootReceiver extends BroadcastReceiver{

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

I've commented out all the lines from run_service() to find out what the cause is, and it's this line

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

It works fine if I call it from within that class (when the application is the foreground), so why can't I call it from inside onReceieve?

Updated to add notification code:

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);


try this way call this way

test.run_service(context);

your run method

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜