<LinearLayout 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:background="@android:color/black" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/title" android:textColor="#0f0" android:textSize="20sp" android:textStyle="bold" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:layout_marginTop="30dp" android:onClick="normal" android:text="@string/notification" android:textColor="#0f0" android:textSize="20sp" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:layout_marginTop="30dp" android:onClick="custom" android:text="@string/custom" android:textColor="#0f0" android:textSize="20sp" /> </LinearLayout>
<?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:background="#000" android:orientation="vertical" > <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="wrap_content" android:contentDescription="@string/action_settings" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textColor="#0f0" android:textSize="15sp" /> </LinearLayout>
package com.itfom.notification;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.RemoteViews;
public class MainActivity extends Activity {
private NotificationManager mNotificationManager;
private Context context;
private Notification notification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//普通的通知
@SuppressWarnings("deprecation")
public void normal(View v){
//创建通知
createNotification("普通的通知");
//把通知放在正在运行栏目中
notification.flags|=Notification.FLAG_ONGOING_EVENT;
//设定默认声音
notification.defaults|=Notification.DEFAULT_SOUND;
//设定默认震动
notification.defaults|=Notification.DEFAULT_VIBRATE;
//设定默认LED灯提醒
notification.defaults|=Notification.DEFAULT_LIGHTS;
//设置点击后通知自动清除
notification.defaults|=Notification.FLAG_AUTO_CANCEL;
String textTitle="Notification示例";
String textContent="程序正在运行,点击此处跳转到演示界面";
Intent it=new Intent(context, MainActivity.class);
PendingIntent pendintent=PendingIntent.getActivity(context, 0, it, 0);
notification.setLatestEventInfo(context, textTitle, textContent, pendintent);
mNotificationManager.notify(0, notification);
}
//自定义的通知
public void custom(View v){
//创建通知
createNotification("个性化的通知");
//自定义通知的声音
notification.sound=Uri.parse(Environment.getExternalStorageDirectory()+"/non.mp3");
//自定义震动参数分别为多长时间开始震动、第一次震动的时间、停止震动的时间
long[] vibrate={0,100,200,300};
notification.vibrate=vibrate;
//自定义闪光灯的方式
notification.ledARGB=0xff00ff00;
notification.ledOnMS=500;
notification.ledOffMS=500;
notification.flags|=Notification.FLAG_SHOW_LIGHTS;
RemoteViews contentView=new RemoteViews(this.getPackageName(),R.layout.notify);
contentView.setTextViewText(R.id.tv, "这是个性化的通知");
//指定个性化的视图
notification.contentView=contentView;
Intent it=new Intent(context, MainActivity.class);
PendingIntent pendintent=PendingIntent.getActivity(context, 0, it, 0);
//指定内容视图
notification.contentIntent=pendintent;
mNotificationManager.notify(1, notification);
}
//自定义一个方法创建通知
@SuppressWarnings("deprecation")
public Notification createNotification(String text){
context = this;
mNotificationManager=(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
int icon=R.drawable.ic_launcher;
long when=System.currentTimeMillis();
return notification = new Notification(icon, text, when);
}
//重写onBackpressed事件
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
//取消通知
mNotificationManager.cancel(0);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有