欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中通知Notification使用實例(振動、燈光、聲音)

 更新時間:2016年01月08日 14:52:30   作者:xu佳佳  
這篇文章主要介紹了Android中通知Notification使用實例,實現(xiàn)振動,燈光,聲音等效果,感興趣的小伙伴們可以參考一下

本文實例講解了通知Notification使用方法,此知識點就是用作通知的顯示,包括振動、燈光、聲音等效果,分享給大家供大家參考,具體內(nèi)容如下

效果圖:



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); 
 
   //創(chuàng)建notification對象來存儲通知所需的各種信息 
   //第一個參數(shù)為圖標 
   //第二個參數(shù)用于指定通知的ticker內(nèi)容 
   //第三個參數(shù)用于指定通知被創(chuàng)建的時間,以毫秒為單位 
   Notification notification = new Notification( 
     R.drawable.ic_launcher, "This is ticker text", 
     System.currentTimeMillis()); 
 
   //此處設置點擊的activity的跳轉(zhuǎn) 
   //第一個參數(shù)依舊是Context 
   //第二個參數(shù)一般用不到,所以用0表示取默認值 
   //第三個參數(shù)就是一個Intent對象 
   //FLAG_CANCEL_CURRENT:如果當前系統(tǒng)中已經(jīng)存在一個相同的PendingIntent對象, 
   // 那么就將先將已有的PendingIntent取消,然后重新生成一個PendingIntent對象。 
   Intent intent = new Intent(this, NotificationActivity.class); 
   PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 
     PendingIntent.FLAG_CANCEL_CURRENT); 
 
   //設置通知的布局 
   //第一個參數(shù)為Context 
   //第二個參數(shù)用于指定通知的標題 
   //第三個參數(shù)用于指定通知的征文內(nèi)容 
   //第四個參數(shù)用于傳入PendingIntent對象,用于設置點擊效果 
   notification.setLatestEventInfo(this, "This is content title", 
     "This is content text", pi); 
 
//   //設置在通知發(fā)出的時候的音頻 
//   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將通知顯示出來 
   //第一個參數(shù)是id,要爆炸為每個通知所指定的id是不同的 
   //第二個參數(shù)就是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="發(fā)出通知" 
  /> 
  
</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> 

相關文章

  • Android修改Dialog樣式的方法

    Android修改Dialog樣式的方法

    Android 對話框支持自定義標題,內(nèi)容,按鈕和點擊事件,基本上可以滿足我們?nèi)粘5氖褂谩?但有時候我們想要修改對話框的文字,按鈕顏色等,系統(tǒng)并沒有提供對應的方法,正常情況下只能自定義布局。 接下來通過源碼解析介紹幾種修改 Dialog樣式的方法。
    2021-05-05
  • Android獲得內(nèi)/外置存儲卡路徑的方法

    Android獲得內(nèi)/外置存儲卡路徑的方法

    我們知道Android上一般都有外置的存儲卡,內(nèi)置存儲卡路徑大家都知道怎么獲得的。那么如何獲取外置存儲卡的位置呢?下面小編通過本文給大家分享下
    2017-01-01
  • Android studio 禁用AndroidX方式

    Android studio 禁用AndroidX方式

    這篇文章主要介紹了Android studio 禁用AndroidX方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Android實現(xiàn)銀行卡號掃描識別功能

    Android實現(xiàn)銀行卡號掃描識別功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)銀行卡號掃描識別功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • Android viewpager自動輪播和小圓點聯(lián)動效果

    Android viewpager自動輪播和小圓點聯(lián)動效果

    這篇文章主要為大家詳細介紹了Android viewpager自動輪播和小圓點聯(lián)動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Android系統(tǒng)中使用shareuserid獲取系統(tǒng)權限的教程

    Android系統(tǒng)中使用shareuserid獲取系統(tǒng)權限的教程

    這篇文章主要介紹了Android系統(tǒng)中使用shareuserid獲取系統(tǒng)權限的教程,這樣以來不同的apk就可以互相訪問對應的app文件夾,需要的朋友可以參考下
    2016-04-04
  • Android OKHttp框架的分發(fā)器與攔截器源碼刨析

    Android OKHttp框架的分發(fā)器與攔截器源碼刨析

    okhttp是一個第三方類庫,用于android中請求網(wǎng)絡。這是一個開源項目,是安卓端最火熱的輕量級框架,由移動支付Square公司貢獻(該公司還貢獻了Picasso和LeakCanary) 。用于替代HttpUrlConnection和Apache HttpClient
    2022-11-11
  • Android 繞過反射黑名單的方法

    Android 繞過反射黑名單的方法

    這篇文章主要介紹了Android 繞過反射黑名單的方法,幫助大家更好的理解和使用Android,感興趣的朋友可以了解下
    2021-02-02
  • Android AIDL中Map參數(shù)傳遞的問題詳解

    Android AIDL中Map參數(shù)傳遞的問題詳解

    這篇文章主要給大家介紹了關于Android AIDL中Map參數(shù)傳遞問題的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友下面來一起看看吧。
    2017-12-12
  • Kotlin文件讀寫與SharedPreferences存儲功能實現(xiàn)方法

    Kotlin文件讀寫與SharedPreferences存儲功能實現(xiàn)方法

    SharedPreferences是安卓平臺上一個輕量級的存儲類,用來保存應用的一些常用配置,比如Activity狀態(tài),Activity暫停時,將此activity的狀態(tài)保存到SharedPereferences中;當Activity重載,系統(tǒng)回調(diào)方法onSaveInstanceState時,再從SharedPreferences中將值取出
    2022-12-12

最新評論