为什么通知会在Android通知栏中显示一段时间然后消失

时间:2012-05-11 02:30:29

标签: android service broadcastreceiver android-pendingintent

我正在使用BroadCastReceiver,其中接收BOOT_COMPLETED行动。收到此广播后,AlarmManager已设置,此AlarmManager会触发通知: 广播接收器的代码:

package pit.opensource.events;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BootReciver extends BroadcastReceiver {

    Context c;
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

    //  Intent i = new Intent(context,SetAlarmService.class);
    //  context.startService(i);
        setAlarm(context);
        Log.d("Alarm","Boot Reciver");

    }
    private void setAlarm(Context c){
        Log.d("Alarm","Set alarm service--------------");
        AlarmManager alarm = (AlarmManager)c.getSystemService(Service.ALARM_SERVICE);


        Calendar cal = Calendar.getInstance();

        cal.setTimeInMillis(System.currentTimeMillis());
        cal.clear();
        cal.set(2012,5,11,5,11);


        Intent i = new Intent(c,ShowNotificationService.class);
        PendingIntent pendingIntent = PendingIntent.getService(c, 12345,i, 0);

        //  alarm.set(AlarmManager.RTC_WAKEUP, dateNotif.getTime(), pendingIntent);
        //alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
        alarm.set(AlarmManager.RTC_WAKEUP, 5000, pendingIntent);


        Log.d("Alarm","Alarm is alreday set*****************");

    }


}

ShowNotificationService的代码:

package pit.opensource.events;

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.util.Log;
import android.widget.Toast;

public class ShowNotificationService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void onStart(Intent intent,int startId){

        Toast.makeText(getApplicationContext(), "Alaraaaaaaaam", Toast.LENGTH_LONG).show();
        Log.d("Alarm","Notification Service");
        showNotification();
    }
    private void showNotification(){
        Intent i = new Intent(getApplicationContext(), DetailsActivity.class);

        Toast.makeText(getApplicationContext(), "Dowanlod finished!", Toast.LENGTH_LONG).show();

        int notificationID = 1;
        NotificationManager nm = (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);

        Log.d("Notification","Notification is set--------------------");
        PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);

        CharSequence tickerText = "Alaraaaaaaaam!";
        long when = System.currentTimeMillis();
        int icon = R.drawable.ic_launcher;

        Notification notification = new Notification(icon,tickerText,when);

        CharSequence contentTitle = "Alarm Alarm Alarm Alarm !!";
        CharSequence contentText = "Please click here to display more details";


        notification.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);

        //---100ms delay, vibrate for 250ms, pause for 100 ms and
        // then vibrate for 500ms---
        notification.vibrate = new long[] { 100, 250, 100, 500}; // Needs vibrate permissions

        nm.notify(notificationID, notification);
    }
}

1 个答案:

答案 0 :(得分:1)

通知消失了,因为在BroadcastReceiver完成后,您的应用程序不再处于活动状态。并且会自动删除来自非活动应用程序的通知。