我正在尝试调用另一个类的方法,但这会导致我的应用程序崩溃。
这就是我在启动时运行它所做的事情
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);
答案 0 :(得分:1)
试试这种方式 这样打电话
test.run_service(context);
你的跑步方法
void run_service(Context context){
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
}