最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2016年01月08日 14:52:30   作者:xu佳佳  
這篇文章主要介紹了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); 
 
   //創(chuàng)建notification對象來存儲通知所需的各種信息 
   //第一個參數為圖標 
   //第二個參數用于指定通知的ticker內容 
   //第三個參數用于指定通知被創(chuàng)建的時間,以毫秒為單位 
   Notification notification = new Notification( 
     R.drawable.ic_launcher, "This is ticker text", 
     System.currentTimeMillis()); 
 
   //此處設置點擊的activity的跳轉 
   //第一個參數依舊是Context 
   //第二個參數一般用不到,所以用0表示取默認值 
   //第三個參數就是一個Intent對象 
   //FLAG_CANCEL_CURRENT:如果當前系統(tǒng)中已經存在一個相同的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); 
 
//   //設置在通知發(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將通知顯示出來 
   //第一個參數是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="發(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> 

相關文章

最新評論

杂多县| 武隆县| 会泽县| 肇州县| 綦江县| 浮梁县| 高阳县| 大宁县| 成安县| 清苑县| 延庆县| 台北县| 沂水县| 二手房| 新密市| 政和县| 清涧县| 依兰县| 历史| 烟台市| 庄浪县| 大关县| 尤溪县| 色达县| 奈曼旗| 临汾市| 沂源县| 淳化县| 崇文区| 南丰县| 房产| 淮安市| 察雅县| 盈江县| 龙游县| 原阳县| 子长县| 水富县| 玉树县| 深水埗区| 屏东市|