Android通知的实现
来源:互联网 发布:淘宝店信誉级别 编辑:程序博客网 时间:2024/05/21 21:38
在安卓开发中 通常会用到通知功能 这一功能主要使用的是android.app.NotificationManager;这一个包中的Notification功能。
NotificationManager notificationManager=(NotificationManager)
getSystemService(NOTIFICATION_SERVICE);调用系统的service。
public class MainActivity extends Activity { protected static final int NOTIFYID_1 = 0; final int CODE=0x17;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); Button button1=(Button)this.findViewById(R.id.button1); final Button button3=(Button)this.findViewById(R.id.button3); button1.setOnClickListener(new OnClickListener() {@SuppressWarnings("deprecation")public void onClick(View v) {Notification notification=new Notification();notification.icon=R.drawable.ic_launcher;notification.tickerText="这是一个通知";notification.when=System.currentTimeMillis(); //设置发送时间notification.defaults=Notification.DEFAULT_ALL; //设置默认的声音和震动以及闪光灯notification.setLatestEventInfo(MainActivity.this, "Hello", "限时特卖了", null);notificationManager.notify(NOTIFYID_1,notification);notification.defaults|=Notification.FLAG_AUTO_CANCEL;Intent it=new Intent(getBaseContext(), MainActivity.class); PendingIntent pendintent=PendingIntent.getActivity(getBaseContext(), 0, it, 0); notificationManager.notify(0, notification);}}); button3.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//notificationManager.cancel(NOTIFYID_1);String user=((EditText)findViewById(R.id.editText1)).getText().toString();Intent intent=new Intent(MainActivity.this,NextActivity.class);Bundle bundle=new Bundle();bundle.putCharSequence("user", user);intent.putExtras( bundle);startActivityForResult(intent, CODE);}}); }protected void onActivityResult(int requestCode,int resultCode,Intent data){super.onActivityResult(requestCode, resultCode, data);if (requestCode==CODE&&resultCode==CODE) {((EditText)findViewById(R.id.editText1)).setText("");((EditText)findViewById(R.id.editText1)).setHint("123");}} @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
Notification notification=new Notification(); //实例化notification
notification.icon=R.drawable.ic_launcher;notification.tickerText="这是一个通知";notification.when=System.currentTimeMillis(); //设置发送时间notification.defaults=Notification.DEFAULT_ALL; //设置默认的声音和震动以及闪光灯notification.setLatestEventInfo(MainActivity.this, "Hello", "限时特卖了", null);notificationManager.notify(NOTIFYID_1,notification);notification.defaults|=Notification.FLAG_AUTO_CANCEL;当然在调用闪光灯和震动功能的时候,需要在manifest中添加权限:
<uses-permission android:name="android.permission.FLASHLIGHT"/> <uses-permission android:name="android.permission.VIBRATE" />
0 0
- 【Android】通知的实现
- Android通知的实现
- android 通知栏的实现
- android 通知栏的实现
- android 通知栏的实现
- android 通知栏的实现
- android 通知栏的实现
- android 通知栏的实现
- android通知栏的实现
- Android推送通知的实现--Android推送通知指南
- Android通知之自定义通知、响应通知上自定义按钮和更新通知的实现
- 【Android】使用Notification实现状态通知栏的通知
- android的通知栏的实现
- Android类似于滚动的通知栏实现
- android 通知栏的实现(转)
- android 通知栏的实现(转)
- android 通知栏的实现(转)
- Android类似于滚动的通知栏实现
- Android Studio 1.5 坑
- 碰到Windows7启动时提示amd_xata.sys签名验证失败的解决办法
- 文本分类
- Launch Screen在iOS7/8中的实现
- Laravel PHP 开发环境配置
- Android通知的实现
- - addChildViewController控制器切换
- java微信网页授权获取用户信息以及JSSDK自定义分享等功能<二>
- iPhone App自动化测试工具总结
- 串口工具选择
- Android Data Binding代码实战
- Symmetric Tree
- 偶然
- 前端框架 - 学习方法