package com.example.lession16_notifi;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 第一步:通过getSystemService()方法得到NotificationManager对象;
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
// 测试
public void test1(View v) {
showNotification("来短信了", "5557", "hello!", R.drawable.ic_launcher,
R.drawable.ic_launcher);
}
// 第二步:对Notification的一些属性进行设置比如:内容,图标,标题,相应notification的动作进行处理等等;
public void showNotification(String tickerText, String contentTitle,
String contentText, int iconId, int notiId) {
// 创建一个Notification
Notification notification = new Notification();
// 设置通知 消息 图标
notification.icon = iconId;
// 设置发出消息的内容
notification.tickerText = tickerText;
// 设置发出通知的时间
notification.when = System.currentTimeMillis();
// 设置显示通知时的默认的发声、振动、Light效果
notification.defaults = Notification.DEFAULT_VIBRATE;// 振动
// Notification notification = new Notification(R.drawable.ic_launcher,"有新的消息", System.currentTimeMillis());
// 3步:PendingIntent android系统负责维护
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,getIntent(), 0);
// 4步:设置更加详细的信息
notification.setLatestEventInfo(this, contentTitle, contentText,pendingIntent);
// 5步:使用notificationManager对象的notify方法 显示Notification消息 需要制定
// Notification的标识
notificationManager.notify(notiId, notification);
}
// 6步:使用notificationManager对象的cancelAll()方法取消
public void clearNoti(View v) {
notificationManager.cancelAll();// 清除所有
}
}
<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=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="22dp"
android:onClick="test1"
android:text="@string/text_notifi" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="60dp"
android:onClick="clearNoti"
android:text="@string/text_clear" />
</RelativeLayout>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有