Andorid Notification--不用启动Activity的通知

来源:互联网 发布:淘宝商品带广告标志 编辑:程序博客网 时间:2024/06/10 22:00

不启动Activity的通知,我目前得到的方法得益于对RemoteView的使用:

代码如下: 

noti_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="3dp"
              >
    <ImageView android:id="@+id/image"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:layout_marginRight="10dp"
              />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#000"
              />
</LinearLayout>


...........................................


java部分代码如下:

    private void MakeNotification2() {
    String tickerText = "tickerText";
    
    NotificationManager NoteManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.icon, tickerText, System.currentTimeMillis());
    
    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.noti_layout);
    contentView.setImageViewResource(R.id.image, R.drawable.icon);
    contentView.setTextViewText(R.id.text, "Hello, this message is in a custom expanded view");
    notification.contentView = contentView;


    
    Intent notificationIntent = new Intent(this, RemoteViews.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.contentIntent = contentIntent;
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_SOUND;
    
    NoteManager.notify(id++, notification);
    }


原创粉丝点击