简体   繁体   English

服务,计时器和通知…,但有错误.. :)

[英]Service, timer and notifications…but with an error.. :)

I'm creating my first service. 我正在创建我的第一个服务。 I have a service with a timer, when the timer run out, if a condition is verified, I want to send a notification. 我有一个带有计时器的服务,当计时器用尽时,如果条件得到验证,我想发送通知。 This is the code: 这是代码:

package app.tdgpisa.service;

import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONArray;
import org.json.JSONException;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import app.tdgpisa.Bacheca;
import app.tdgpisa.R;
import app.tdgpisa.db.Database;

public class NotificaBacheca extends Service {
    private Timer miotimer = new Timer();
    private static  long intervallo = 10*1000;
    private long msg =0;
    Database database=new Database();
    private static final int SIMPLE_NOTIFICATION_ID = 1;
    NotificationManager mNotificationManager;

    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {

        Log.d("notifica", " inizio create");
        String messaggi = database.MessaggiNum();

        try {
            JSONArray jsonArray = new JSONArray(messaggi);
            for (int i = 0; i < jsonArray.length(); i++)
            {
                msg = jsonArray.getJSONObject(i).getInt("NUM");
                Log.d("notifica", "numero msg nel create: "+ msg);
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void onStart(Intent intent, int startid) {
        StartServUpdateTask();
    }    

    private void StartServUpdateTask() {
        miotimer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                Log.d("notifica", "parte il run");
                String messaggi = database.MessaggiNum();
                JSONArray jsonArray;
                try {
                    jsonArray = new JSONArray(messaggi);
                    long newmsg = jsonArray.getJSONObject(0).getLong("NUM");
                    Log.d("notifica", "numero msg nel run: "+ newmsg);
                    if (newmsg>msg){
                        msg=newmsg;
                        Log.d("notifica", "sono diversi, parte la notifica");
                        Notifica();
                    }
                } catch (JSONException e) {

                }

            }
        }, 0, intervallo);
    }
    private void Notifica() {

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(NotificaBacheca.this);

        notificationBuilder.setContentTitle("TITOLO");
        notificationBuilder.setContentText("TESTO");
        notificationBuilder.setTicker("SCROLL TESTO");
        notificationBuilder.setWhen(System.currentTimeMillis());
        notificationBuilder.setSmallIcon(R.drawable.ic_launcher);
        notificationBuilder.setAutoCancel(true);

        Intent notificationIntent = new Intent(NotificaBacheca.this, Bacheca.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notificationBuilder.setContentIntent(contentIntent);

        notificationBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);

        mNotificationManager.notify(SIMPLE_NOTIFICATION_ID, notificationBuilder.build());
    }


}

And this is the error: 这是错误:

11-19 13:51:56.752: E/AndroidRuntime(343): FATAL EXCEPTION: Timer-0
11-19 13:51:56.752: E/AndroidRuntime(343): java.lang.NullPointerException
11-19 13:51:56.752: E/AndroidRuntime(343):  at app.tdgpisa.service.NotificaBacheca.Notifica(NotificaBacheca.java:104)
11-19 13:51:56.752: E/AndroidRuntime(343):  at app.tdgpisa.service.NotificaBacheca.access$2(NotificaBacheca.java:87)
11-19 13:51:56.752: E/AndroidRuntime(343):  at app.tdgpisa.service.NotificaBacheca$1.run(NotificaBacheca.java:78)
11-19 13:51:56.752: E/AndroidRuntime(343):  at java.util.Timer$TimerImpl.run(Timer.java:284)

Can anyone help me? 谁能帮我? Sorry for, eventualy, big mistake... :) Thank you! 抱歉,最终会出现重大错误... :)谢谢!

mNotificationManager is null. mNotificationManager为空。 It was never intialized to anything. 它从未被初始化为任何东西。 The null pointer line number should have told you everything you needed. 空指针行号应该已经告诉了您所需的一切。

Your mNotificationManager is never set, but you call mNotificationManager.notify(SIMPLE_NOTIFICATION_ID, notificationBuilder.build()); 您的mNotificationManager从未设置,但您调用mNotificationManager.notify(SIMPLE_NOTIFICATION_ID, notificationBuilder.build()); on line 104. 在第104行。

You should set mNotificationManager before. 您应该先设置mNotificationManager。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM