如何从服务发出通知

时间:2016-12-18 15:59:12

标签: android service notifications

使用以下代码,我尝试从服务发出通知。不明白为什么,但我没有得到任何通知。到达了通知代码,但它什么也没有产生。

public class MyIntentService extends IntentService {
private static final int NOTIFICATION_ID = 1;
String temp;
int MAX_TRIES = 5;
public MyIntentService() {
    super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
    if (intent != null) {
        Bundle a = intent.getExtras();
        String id = a.getString("rID");
        Timer t = new Timer();
        t.schedule(new pollingTask(id),0,5000);

        }
    }


class pollingTask extends TimerTask {
    String rID;

    protected pollingTask(String rid) {
        super();
        this.rID = rid;
        temp="";
        MAX_TRIES -= 1;
    }
    @Override
    public void run() {
            if(temp.length()>1) {
                Notification notification = new    
Notification.Builder(getApplicationContext())
                        .setContentTitle(getString(R.string.app_name))
                        .setContentText("testing")
                        .setSmallIcon(R.drawable.star20)
                        .setWhen(System.currentTimeMillis())
       //                 .setContentIntent(contentIntent)
                        .build();
                startForeground(NOTIFICATION_ID, notification);
                    this.cancel();
                    stopSelf();
            } else temp = WebService.getServerResultsSERVICE(this.rID,    
"getResults");

        }
    }
}

请协助,

0 个答案:

没有答案