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

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

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

本文實(shí)例講解了通知Notification使用方法,此知識點(diǎn)就是用作通知的顯示,包括振動、燈光、聲音等效果,分享給大家供大家參考,具體內(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ù)為圖標(biāo) 
   //第二個參數(shù)用于指定通知的ticker內(nèi)容 
   //第三個參數(shù)用于指定通知被創(chuàng)建的時間,以毫秒為單位 
   Notification notification = new Notification( 
     R.drawable.ic_launcher, "This is ticker text", 
     System.currentTimeMillis()); 
 
   //此處設(shè)置點(diǎn)擊的activity的跳轉(zhuǎn) 
   //第一個參數(shù)依舊是Context 
   //第二個參數(shù)一般用不到,所以用0表示取默認(rèn)值 
   //第三個參數(shù)就是一個Intent對象 
   //FLAG_CANCEL_CURRENT:如果當(dāng)前系統(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è)置通知的布局 
   //第一個參數(shù)為Context 
   //第二個參數(shù)用于指定通知的標(biāo)題 
   //第三個參數(shù)用于指定通知的征文內(nèi)容 
   //第四個參數(shù)用于傳入PendingIntent對象,用于設(shè)置點(diǎn)擊效果 
   notification.setLatestEventInfo(this, "This is content title", 
     "This is content text", pi); 
 
//   //設(shè)置在通知發(fā)出的時候的音頻 
//   Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones/Basic_tone.ogg")); 
//   notification.sound = soundUri; 
// 
//   //設(shè)置手機(jī)震動 
//   //第一個,0表示手機(jī)靜止的時長,第二個,1000表示手機(jī)震動的時長 
//   //第三個,1000表示手機(jī)震動的時長,第四個,1000表示手機(jī)震動的時長 
//   //此處表示手機(jī)先震動1秒,然后靜止1秒,然后再震動1秒 
//   long[] vibrates = {0, 1000, 1000, 1000}; 
//   notification.vibrate = vibrates; 
// 
//   //設(shè)置LED指示燈的閃爍 
//   //ledARGB設(shè)置顏色 
//   //ledOnMS指定LED燈亮起的時間 
//   //ledOffMS指定LED燈暗去的時間 
//   //flags用于指定通知的行為 
//   notification.ledARGB = Color.GREEN; 
//   notification.ledOnMS = 1000; 
//   notification.ledOffMS = 1000; 
//   notification.flags = Notification.FLAG_SHOW_LIGHTS; 
 
   //如果不想進(jìn)行那么多繁雜的這只,可以直接使用通知的默認(rèn)效果 
   //默認(rèn)設(shè)置了聲音,震動和燈光 
   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后把通知給關(guān)掉 
  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="這是通知點(diǎn)擊后的界面" 
  /> 
  
</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> 

相關(guān)文章

最新評論

石城县| 华池县| 十堰市| 永德县| 福贡县| 汉中市| 长兴县| 都兰县| 广东省| 广灵县| 中方县| 尖扎县| 大田县| 施秉县| 鲜城| 榕江县| 靖边县| 金山区| 曲靖市| 邢台市| 凤山县| 宾川县| 太保市| 小金县| 库车县| 武安市| 穆棱市| 哈尔滨市| 怀来县| 额济纳旗| 项城市| 凤凰县| 新巴尔虎左旗| 马尔康县| 白水县| 卓尼县| 元谋县| 永州市| 金坛市| 平塘县| 朝阳区|