使用Android服务通知

时间:2015-05-08 14:15:29

标签: android service notifications broadcastreceiver

我有一个以广播接收者开始的服务。

在这项服务中,我想每分钟发送一次通知。 问题是:我只收到一次消息。

问题:我需要做什么,每分钟收到一条消息?

这是我的代码。

的Manifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />      
<application>
    <receiver android:name="com.polifrete.polifreteFunctions.MyReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
        </intent-filter>
    </receiver>
    <service
        android:name="com.polifrete.polifreteFunctions.NotificationService"
        android:launchMode="singleTask" >
    </service>
</application>

服务

package com.polifrete.polifreteFunctions;

import com.polifrete.polifreteandroid.MainActivity;

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;

public class NotificationService extends Service {

public static final int time = 60 * 1000;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    Log.i("Script", "Passou SERVICE CREATE");
    super.onCreate();
}

@Override
public void onStart(Intent intent, int startId) {
    Log.i("Script", "Passou SERVICE START");
}

@Override
public void onDestroy() {
    Log.i("Script", "Passou SERVICE DESTROY");
    super.onDestroy();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("Script", "Passou SERVICE COMMAND");
    final Thread t = new Thread() {
        @Override
        public void run() {
            try {
                Thread.sleep(time);
                showNotification();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    t.start();
    return Service.START_STICKY;
}

public void showNotification() {

    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder n = new NotificationCompat.Builder(this)
            .setContentTitle("Test Notification")
            .setContentText("Test Notification.")
            .setSmallIcon(
                    com.polifrete.polifreteandroid.R.drawable.ic_actionbar)
            .setContentIntent(pIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    notificationManager.notify(0, n.build());

}

}

BROADCAST

package com.polifrete.polifreteFunctions;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Log.i("Script", "Passou RECEIVER");
    Intent i = new Intent(context, NotificationService.class);
    context.startService(i);
}

}

2 个答案:

答案 0 :(得分:0)

您在行notificationManager.notify(0, n.build());中设置了相同的通知ID = 0。设置任意随机值((int)(Math.random()*10000);)。相同的ID会导致覆盖具有相同ID的现有通知。 另外,您应该在不同PendingIntent s

的下面设置行
intent.setAction(Long.toString(System.currentTimeMillis()));
...
notificationManager.notify((int)(Math.random()*10000), n.build());

你也应该使用TimerCountDownTimer代替睡觉Thread

答案 1 :(得分:0)

你应该改成这个

  while(true){

        try {
            Thread.sleep(time);
            showNotification();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
   }

如果您使用相同的通知ID,则下一个通知将只替换上一个通知,如果您希望它们分开,只需使用其他ID进行下一次通知。