Notification示例(Project)

1、

package leadingmis.mobile.main;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class NotificationActivity extends Activity {
    private Button notifyBtn;
    private Button cancelBtn;
    private NotificationManager nm;
    private Notification n;
    // 通知的ID
    public static final int ID = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notification);
        notifyBtn = (Button) findViewById(R.id.notify);
        cancelBtn = (Button) findViewById(R.id.cancel);
        notifyBtn.setOnClickListener(new MyOnClickListener());
        cancelBtn.setOnClickListener(new MyOnClickListener());
        // 1.获取NotificationManager对象
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // 2.初始化Notification对象
        n = new Notification();
        n.flags = Notification.FLAG_ONGOING_EVENT;
        // 设置通知的icon
        n.icon = R.drawable.notify;
        // 设置通知在状态栏上显示的滚动信息
        n.tickerText = "一个通知";
        // 设置通知的时间
        n.when = System.currentTimeMillis();
    }
    class MyOnClickListener implements OnClickListener{
     @Override
     public void onClick(View v) {
      switch (v.getId()) {
      case R.id.notify:
       // 3.设置通知的显示参数
       Intent intent = new Intent(NotificationActivity.this, NotificationView.class);
       PendingIntent pi = PendingIntent.getActivity(NotificationActivity.this, 0, intent, 0);
       n.setLatestEventInfo(NotificationActivity.this, "通知标题", "通知内容", pi);
       // 4.发送通知
       nm.notify(ID, n);
       break;
   case R.id.cancel:
    // 取消通知
    nm.cancel(ID);
    break;
   }
     }
    }
}

2、

package leadingmis.mobile.main;

import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;

/**
 * 打开通知后跳转的Activity
 * @author zuolongsnail
 */
public class NotificationView extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.notificationview);
  // 取消通知
  NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  nm.cancel(NotificationActivity.ID);
 }
}

3、

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="sdf"
    />  
     <Button android:id="@+id/notify" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:text="发出通知" />
 <Button android:id="@+id/cancel" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:text="取消通知" />
</LinearLayout>

4、

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TextView android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:text="打开通知后跳转的Activity" />
</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值