计时器上的Android通知

时间:2013-07-02 23:06:49

标签: java android

我正在尝试通过计时器运行通知,即使我的应用程序关闭后也会继续运行。 我知道它的错误,但我不确定我应该改变什么才能让它正确运行。 任何帮助将不胜感激!

@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):at com.codalata.nickx.Home $ 1.run(Home.java:104) 07-02 19:00:16.788:E / AndroidRuntime(2086):at java.util.Timer $ TimerImpl.run(Timer.java:284)

0 个答案:

没有答案