消息提醒,初探Notification
2014-10-25 11:15
127 查看
1:MainActivity.java
2:activity_main.xml
3:NotificationActivity.java
4:activity_notification.xml
package com.example.notificationtest; 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.support.v4.app.NotificationCompat; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private static int NOTIFICATION_ID = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnSendNotification = (Button)findViewById(R.id.btnSendNotification); btnSendNotification.setOnClickListener(btnOnClickListener()); } OnClickListener btnOnClickListener(){ return new OnClickListener(){ @Override public void onClick(View arg0) { switch(arg0.getId()){ case R.id.btnSendNotification: Intent intent = new Intent(MainActivity.this, NotificationActivity.class); String title = "消息标题"; intent.putExtra("title", title); PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification n = new NotificationCompat.Builder(MainActivity.this) .setSmallIcon(R.drawable.ic_launcher) .setTicker("消息提示").setContentTitle(title) .setContentText("消息子标题") .setContentInfo("消息内容") .setWhen(System.currentTimeMillis()) //显示当前时间 .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE) //使用默认的声音与震动 .setContentIntent(pi) .setAutoCancel(true) .build(); nm.notify(++NOTIFICATION_ID, n); break; } } }; } }
2:activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.notificationtest.MainActivity$PlaceholderFragment" > <Button android:id="@+id/btnSendNotification" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/send_notification" /> </RelativeLayout>
3:NotificationActivity.java
package com.example.notificationtest; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class NotificationActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); TextView tvInfo = (TextView)findViewById(R.id.tvInfo); tvInfo.setText(getIntent().getStringExtra("title")); } }
4:activity_notification.xml
<?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:orientation="vertical" android:gravity="center"> <TextView android:id="@+id/tvInfo" android:textSize="22sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Notificaiton Info"/> </LinearLayout>
相关文章推荐
- express redis socket 消息提醒方案:本地emit 轮循服务器获取redis 再推送
- 计时时钟和消息提醒(第二版)
- 安卓数字消息提醒、角标-测试BadgeView效果
- 类似于QQ新消息提醒-前端
- qq列表拖拽效果 qq果冻效果 qq删除消息提醒 小红点 CAShapeLayer动画
- angular定制组件-toastr(消息提醒)
- 消息提醒显示
- Android消息个数提醒控件使用详解
- 安卓实现消息提醒(震动和提示音)
- 25 个超棒的 jQuery 消息提醒插件
- 微博推、拉结合按时间分区消息提醒模式的学习
- 桌面右下角消息提醒
- PHP实现RTX发送消息提醒
- RTX发送消息提醒实现以及注意事项
- 关于cookie的部分用法(页面中新消息声音提醒开关)
- Android系统 小米/三星/索尼 应用启动图标未读消息数(BadgeNumber)动态提醒
- express redis socket 消息提醒方案2:订阅redis,推送消息
- swing消息提醒
- Android高仿微信5.2.1主界面及消息提醒
- Viewbadger消息提醒的实现