Notifycation使用入门

notifycation有两种使用情况,第一种是只发消息,第二种是自定义布局显示,并且还可以对交互事件进行相应,类似于很多音乐播放器界面退出之后在通知栏显示简单播放条,可以控制。

先发自定的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/img_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/basketball"/>

    <TextView
        android:id="@+id/txt_notify"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:textSize="20sp" />

</LinearLayout>

然后是service:

public class MyService extends Service {
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String info = intent.getExtras().getString("info");
        Log.i("notifycation 服务", info);
        return super.onStartCommand(intent, flags, startId);
    }
}

主程序代码:

/**
 * notifycation的简单实现
 */
public class MainActivity extends AppCompatActivity {
    private static final int NOTIFYCATION_FLAG=1;

    @Bind(R.id.btn_notify)
    Button btnNotify;
    @Bind(R.id.btn_notify_custom)
    Button btnNotifyCustom;

    private NotificationManager manager;//notifycation管理器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
    }

    /**
     * 初始化
     */
    private void initView() {
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }

    @OnClick({R.id.btn_notify,R.id.btn_notify_custom})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_notify:
                sendNotify();
                break;
            case R.id.btn_notify_custom:
                sendNotifyCustom();
                break;
            default:
                break;
        }
    }

    /**
     * 发送自定义的notify
     * 包括布局文件和监听事件
     */
    private void sendNotifyCustom() {
        Intent intent=new Intent(this,MainActivity.class);
        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);

        Intent svcIntent=new Intent(this,MyService.class);
        Bundle bundle=new Bundle();
        bundle.putString("info","notify点击事件传递给service的参数");
        svcIntent.putExtras(bundle);
        //这里可以用服务,可以用广播,也可以用activity进行跳转
        PendingIntent serviceIntent=PendingIntent.getService(this,0,svcIntent,0);

        RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.layout_notify_info);
        remoteViews.setTextViewText(R.id.txt_notify,"这是控件显示的notify消息");
        remoteViews.setOnClickPendingIntent(R.id.img_icon,serviceIntent);

        Notification notification=new Notification.Builder(this)
                .setSmallIcon(R.drawable.basketball)
                .setTicker("你有新消息了")
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent)
                .setContent(remoteViews)
                .setNumber(1)
                .build();

        notification.flags|=Notification.FLAG_AUTO_CANCEL;
        manager.notify(NOTIFYCATION_FLAG,notification);
    }

    /**
     * 发送简单notify
     */
    private void sendNotify() {
        Intent intent=new Intent(this,MainActivity.class);
        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);

        Notification notification=new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setTicker("你有新消息了")
                .setContentTitle("notify Title")
                .setContentText("这是notify的内容")
                .setContentIntent(pendingIntent)
                .setNumber(1)
                .build();

        notification.flags|=Notification.FLAG_AUTO_CANCEL;
        manager.notify(NOTIFYCATION_FLAG,notification);
    }
}

运行效果:

简单通知:


自定义通知:



点击图片之后的反应,调用服务打印日志:




原设想做音乐播放器关闭界面之后让她继续播放并可以控制的技术关算是通过了。下来该尝试一下了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值