Notifycation使用入门
来源:互联网 发布:lg扫地机器人.知乎 编辑:程序博客网 时间:2024/06/01 10:28
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
- Notifycation使用入门
- Notifycation(通知)的使用
- notifycation通知推送的使用
- android中的notifycation
- 在日历的下拉通知栏Notifycation点击忽略无效
- Quartz使用-入门使用
- Android 点击桌面快捷方式和Notifycation跳转到Task栈顶Activity
- Android 点击桌面快捷方式和Notifycation跳转到Task栈顶Activity
- Android 点击桌面快捷方式和Notifycation跳转到Task栈顶Activity
- Android 点击桌面快捷方式和Notifycation跳转到Task栈顶Activity
- Eclipse使用入门 (1)
- Eclipse使用入门【转载】
- Eclipse使用入门
- Toad 使用快速入门
- JSTL使用入门
- CxImage类使用入门
- Apache James使用入门
- Toad 使用快速入门
- TEC1401.Report开发技术总结 - 第九章 使用BI Publisher开发报表-使用BI Publisher创建RTF模板的语法(4/5)
- HDUoj 2141 Can you find it?(排序+二分)
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 272. Closest Binary Search Tree Value II (Hard) (cpp)
- TEC1401.Report开发技术总结 - 第十章 使用BI Publisher开发报表-其他注意事项(5/5)
- Notifycation使用入门
- Subsets
- 【LeetCode】 475. Heaters
- Leetcode 285. Inorder Successor in BST (Medium) (cpp)
- 简单几步实现网络音乐播放器(Python爬虫版百度FM)
- 使用C++扩展Python的功能
- Leetcode 298. Binary Tree Longest Consecutive Sequence (Medium) (cpp)
- Java的堆内存与栈内存的异同(Heap memory vs Stack memory)
- python的未来-pypy