我正在尝试在计时器上运行通知,即使在我的应用程序关闭后仍继续运行。我知道它是错误的,但不确定我应该改变什么以使其正常运行。任何帮助,将不胜感激!
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.home);
Timer timer = new Timer();
TimerTask task = new TimerTask()
{
@SuppressWarnings("deprecation")
@Override
public void run()
{
Intent intent = new Intent(Home.this, Home.class);
PendingIntent pi = PendingIntent.getActivity(Home.this, 0, intent, 0);
String body = "This is a standard message!";
String title = "Nick C.";
Notification n = new Notification(R.drawable.heart, body, System.currentTimeMillis());
n.setLatestEventInfo(Home.this, title, body, pi);
n.defaults = Notification.DEFAULT_ALL;
nm.notify(uniqueID, n);
finish();
}
}; timer.schedule(task, 0, 15000);
07-02 19:00:16.788: E/AndroidRuntime(2086): 致命异常: Timer-0 07-02 19:00:16.788: E/AndroidRuntime(2086): java.lang.NullPointerException 07-02 19:00: 16.788: E/AndroidRuntime(2086): 在 com.codalata.nickx.Home$1.run(Home.java:104) 07-02 19:00:16.788: E/AndroidRuntime(2086): 在 java.util.Timer$ TimerImpl.run(Timer.java:284)