当前位置: 首页 > 编程笔记 >

Android中通知Notification使用实例(振动、灯光、声音)

唐博文
2023-03-14
本文向大家介绍Android中通知Notification使用实例(振动、灯光、声音),包括了Android中通知Notification使用实例(振动、灯光、声音)的使用技巧和注意事项,需要的朋友参考一下

本文实例讲解了通知Notification使用方法,此知识点就是用作通知的显示,包括振动、灯光、声音等效果,分享给大家供大家参考,具体内容如下

效果图:



MainActivity:

import java.io.File; 
 
import android.app.Activity; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Intent; 
import android.graphics.Color; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
 
public class MainActivity extends Activity implements OnClickListener { 
 
 private Button sendNotice; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
  sendNotice = (Button) findViewById(R.id.send_notice); 
  sendNotice.setOnClickListener(this); 
 } 
 
 @Override 
 public void onClick(View v) { 
  switch (v.getId()) { 
  case R.id.send_notice: 
   NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
 
   //创建notification对象来存储通知所需的各种信息 
   //第一个参数为图标 
   //第二个参数用于指定通知的ticker内容 
   //第三个参数用于指定通知被创建的时间,以毫秒为单位 
   Notification notification = new Notification( 
     R.drawable.ic_launcher, "This is ticker text", 
     System.currentTimeMillis()); 
 
   //此处设置点击的activity的跳转 
   //第一个参数依旧是Context 
   //第二个参数一般用不到,所以用0表示取默认值 
   //第三个参数就是一个Intent对象 
   //FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的PendingIntent对象, 
   // 那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。 
   Intent intent = new Intent(this, NotificationActivity.class); 
   PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 
     PendingIntent.FLAG_CANCEL_CURRENT); 
 
   //设置通知的布局 
   //第一个参数为Context 
   //第二个参数用于指定通知的标题 
   //第三个参数用于指定通知的征文内容 
   //第四个参数用于传入PendingIntent对象,用于设置点击效果 
   notification.setLatestEventInfo(this, "This is content title", 
     "This is content text", pi); 
 
//   //设置在通知发出的时候的音频 
//   Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones/Basic_tone.ogg")); 
//   notification.sound = soundUri; 
// 
//   //设置手机震动 
//   //第一个,0表示手机静止的时长,第二个,1000表示手机震动的时长 
//   //第三个,1000表示手机震动的时长,第四个,1000表示手机震动的时长 
//   //此处表示手机先震动1秒,然后静止1秒,然后再震动1秒 
//   long[] vibrates = {0, 1000, 1000, 1000}; 
//   notification.vibrate = vibrates; 
// 
//   //设置LED指示灯的闪烁 
//   //ledARGB设置颜色 
//   //ledOnMS指定LED灯亮起的时间 
//   //ledOffMS指定LED灯暗去的时间 
//   //flags用于指定通知的行为 
//   notification.ledARGB = Color.GREEN; 
//   notification.ledOnMS = 1000; 
//   notification.ledOffMS = 1000; 
//   notification.flags = Notification.FLAG_SHOW_LIGHTS; 
 
   //如果不想进行那么多繁杂的这只,可以直接使用通知的默认效果 
   //默认设置了声音,震动和灯光 
   notification.defaults = Notification.DEFAULT_ALL; 
 
   //使用notify将通知显示出来 
   //第一个参数是id,要爆炸为每个通知所指定的id是不同的 
   //第二个参数就是Notification对象 
   manager.notify(1, notification); 
   break; 
  default: 
   break; 
  } 
 } 
 
} 

activity_main:

<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:orientation="vertical" > 
 
 <Button 
  android:id="@+id/send_notice" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:text="发出通知" 
  /> 
  
</LinearLayout> 

NotificationActivity:

import android.app.Activity; 
import android.app.NotificationManager; 
import android.os.Bundle; 
 
public class NotificationActivity extends Activity { 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.notification_layout); 
 
  //打开NotificationActivity这个Activity后把通知给关掉 
  NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
  manager.cancel(1); 
 } 
 
} 

notification_layout:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" > 
  
 <TextView 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_centerInParent="true" 
  android:textSize="24sp" 
  android:text="这是通知点击后的界面" 
  /> 
  
</RelativeLayout> 

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
 package="com.example.notificationtest" 
 android:versionCode="1" 
 android:versionName="1.0" > 
 
 <uses-sdk 
  android:minSdkVersion="14" 
  android:targetSdkVersion="19" /> 
 
 <application 
  android:allowBackup="true" 
  android:icon="@drawable/ic_launcher" 
  android:label="@string/app_name" 
  android:theme="@style/AppTheme" > 
  <activity 
   android:name="com.example.notificationtest.MainActivity" 
   android:label="@string/app_name" > 
   <intent-filter> 
    <action android:name="android.intent.action.MAIN" /> 
 
    <category android:name="android.intent.category.LAUNCHER" /> 
   </intent-filter> 
  </activity> 
  <activity android:name=".NotificationActivity" > 
  </activity> 
 
 </application> 
 
</manifest> 
 类似资料:
  • 我正在从firebase向我的Android应用程序发送推送通知。但当我的应用程序在后台时,不会调用firebase onMessageReceived方法,而是firebase向系统发送通知,以便在系统托盘中显示通知。通知出现在系统托盘中,但没有通知声音,即使我在系统设置中允许应用程序发出通知声音。这是我的通知代码谢谢

  • 本文向大家介绍Android使用Notification实现普通通知栏(一),包括了Android使用Notification实现普通通知栏(一)的使用技巧和注意事项,需要的朋友参考一下 Notification是在你的应用常规界面之外展示的消息。当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏。要查看消息的详情需要进入通知抽屉(notificationdrawer)中查看。(n

  • 本文向大家介绍android中创建通知栏Notification代码实例,包括了android中创建通知栏Notification代码实例的使用技巧和注意事项,需要的朋友参考一下

  • 本文向大家介绍Android中通知Notification的使用方法,包括了Android中通知Notification的使用方法的使用技巧和注意事项,需要的朋友参考一下 每个使用Android手机的人应该对Android中的通知不陌生,下面我们就学习一下怎么使用Android中的通知。 一、通知的基本用法 活动、广播接收器和服务中都可以创建通知,由于我们一般在程序进入后台后才使用通知,所以真实场

  • 我注意到Flatter firebase\u消息包自动处理设备的推送通知。但是,有没有办法通过编程启用或禁用通知声音和振动?我想在我的应用程序设置中提供一个开关盒,用户可以选择在收到通知时是否希望设备发出声音和/或振动。

  • 本文向大家介绍详解Android中使用Notification实现进度通知栏(示例三),包括了详解Android中使用Notification实现进度通知栏(示例三)的使用技巧和注意事项,需要的朋友参考一下 我们在使用APP的过程中,软件会偶尔提示我们进行版本更新,我们点击确认更新后,会在通知栏显示下载更新进度(已知长度的进度条)以及安装情况(不确定进度条),这就是我们今天要实现的功能。实现效果如