android服务通知栏

时间:2012-02-27 02:10:46

标签: android service notifications android-notification-bar

我正在尝试创建一个集中的通知栏系统。我正在使用3行输出的自定义通知。需要从多个活动和服务(如果已启用)调用此通知。

每项活动都是更新其特定通知行,同时不更改其他两行通知数据。

如何从服务中调用此代码?

如何取消任何课程的通知?它可能不知道它正在运行。

通知类代码:

public class notifyUser extends Activity
{
    private NotificationManager mManager;

    private static int APP_ID_LIST = 999;

    private String  pMess1 = "" ;
    private String  pMess2 = "" ;
    private String  pMess3 = "" ;
    // -------------------------------------------------------------------
    // constructor
    // -------------------------------------------------------------------
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        notify_user();

        finish();
    }

    // --------------------------------------------------------------------
    // set notification if needed
    // --------------------------------------------------------------------
    private void notify_user()
    {
        pMess1 = myjdb.get_prefs("pMess1") ;
        pMess2 = myjdb.get_prefs("pMess2") ;
        pMess3 = myjdb.get_prefs("pMess3") ;


        Notification notification = new Notification(R.drawable.gtracker_icon,
                "Notify", System.currentTimeMillis());

        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);

        contentView.setImageViewResource(R.id.ivImage, R.drawable.gtracker_icon);
        contentView.setTextViewText(R.id.tvText1, pMess1 );
        contentView.setTextViewText(R.id.tvText2, pMess2 );
        contentView.setTextViewText(R.id.tvText3, pMess3 );

        notification.contentView = contentView;

        final Intent notificationIntent  = new Intent(this, Shop.class);

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );

        mManager = (NotificationManager) getSystemService(Shop.NOTIFICATION_SERVICE);

        notification.flags |= Notification.FLAG_NO_CLEAR;
        notification.flags |= Notification.FLAG_ONGOING_EVENT;

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.contentIntent = contentIntent;

        mManager.notify(APP_ID_LIST, notification);
    }
}

0 个答案:

没有答案