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

詳解Android中Notification的使用方法

 更新時(shí)間:2015年12月24日 11:45:26   作者:cjjky  
這篇文章主要介紹了Android中Notification的使用方法,最典型的應(yīng)用就是未看短信和未接來(lái)電的顯示,還有QQ微信,想要深入了解Notification的朋友可以參考本文

      在消息通知的時(shí)候,我們經(jīng)常用到兩個(gè)控件Notification和Toast。特別是重要的和需要長(zhǎng)時(shí)間顯示的信息,用Notification最合適不過(guò)了。他可以在頂部顯示一個(gè)圖標(biāo)以標(biāo)示有了新的通知,當(dāng)我們拉下通知欄的時(shí)候,可以看到詳細(xì)的通知內(nèi)容。
      最典型的應(yīng)用就是未看短信和未接來(lái)電的顯示,還有QQ微信,我們一看就知道有一個(gè)未接來(lái)電或者未看短信,收到QQ離線信息。同樣,我們也可以自定義一個(gè)Notification來(lái)定義我們自己的程序想要傳達(dá)的信息。

Notification我把他分為兩種,一種是默認(rèn)的顯示方式,另一種是自定義的,今天為大家講述默認(rèn)的顯示方式
1、程序框架結(jié)構(gòu)圖如下


2、布局文件 main.xml 源碼如下

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 > 
<TextView  
 android:layout_width="fill_parent"  
 android:layout_height="wrap_content"  
 android:gravity="center" 
 android:textColor="#EEE" 
 android:textStyle="bold" 
 android:textSize="25sp" 
 android:text="NotificationDemo實(shí)例" /> 
<Button 
 android:id="@+id/btnSend" 
 android:text="send notification" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_gravity="center"/>  
</LinearLayout> 

3、MainActivity.java源碼如下:

package com.andyidea.notification; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
 
public class MainActivity extends Activity { 
 private Button btnSend; 
  
 //定義BroadcastReceiver的action 
 private static final String NotificationDemo_Action = "com.andyidea.notification.NotificationDemo_Action"; 
  
 /** Called when the activity is first created. */ 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.main); 
   
  btnSend = (Button)findViewById(R.id.btnSend); 
  btnSend.setOnClickListener(new View.OnClickListener() { 
   @Override 
   public void onClick(View v) { 
    Intent intent = new Intent(); 
    intent.setAction(NotificationDemo_Action); 
    sendBroadcast(intent); 
   } 
  }); 
 } 
  
} 

4、布局文件 secondlayou.xml 源碼如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent"> 
 <TextView  
 android:layout_width="fill_parent"  
 android:layout_height="wrap_content"  
 android:gravity="center" 
 android:textColor="#EEE" 
 android:textStyle="bold" 
 android:textSize="25sp" 
 android:text="顯示通知界面" /> 
<Button 
 android:id="@+id/btnCancel" 
 android:text="cancel notification" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_gravity="center" />  
</LinearLayout> 

5、SecondActivity.java源碼如下:

package com.andyidea.notification; 
 
import android.app.Activity; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
 
public class SecondActivity extends Activity { 
 
 private Button btnCancel; 
 //聲明Notification 
 private Notification notification; 
 //聲明NotificationManager 
 private NotificationManager mNotification; 
 //標(biāo)識(shí)Notification的ID 
 private static final int ID = 1; 
  
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.secondlayout); 
   
  btnCancel = (Button)findViewById(R.id.btnCancel); 
  //怎樣獲得NotificationManager的實(shí)例? 
  String service = NOTIFICATION_SERVICE; 
  mNotification = (NotificationManager)getSystemService(service); 
   
  //獲得Notification的實(shí)例 
  notification = new Notification(); 
   
  //設(shè)置該圖標(biāo) 會(huì)在狀態(tài)欄顯示 
  int icon = notification.icon = android.R.drawable.stat_sys_phone_call; 
  //設(shè)置提示信息 
  String tickerText = "Test Notification"; 
  //設(shè)置顯示時(shí)間 
  long when = System.currentTimeMillis(); 
  notification.icon = icon; 
  notification.tickerText = tickerText; 
  notification.when = when; 
   
  Intent intent = new Intent(this, MainActivity.class); 
  PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0); 
  notification.setLatestEventInfo(this, "消息", "SMS Android", pi); 
  mNotification.notify(ID, notification); 
   
  btnCancel.setOnClickListener(new View.OnClickListener() { 
   @Override 
   public void onClick(View v) { 
    mNotification.cancel(ID); //--->取消通知 
   } 
  }); 
 } 
  
} 

6、NotificationReceiver.java源碼如下:

package com.andyidea.notification; 
 
import com.andyidea.notification.SecondActivity; 
 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
 
public class NotificationReceiver extends BroadcastReceiver { 
 
 @Override 
 public void onReceive(Context context, Intent intent) { 
  //實(shí)例化Intent 
  Intent i = new Intent(); 
  //在新任務(wù)中啟動(dòng)Activity 
  i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
  //設(shè)置Intent啟動(dòng)的組件名稱 
  i.setClass(context, SecondActivity.class); 
  //啟動(dòng)Activity,顯示通知 
  context.startActivity(i); 
 } 
 
} 

7、程序運(yùn)行效果如下:

以上就是針對(duì)Android中Notification使用方法進(jìn)行的詳細(xì)介紹,希望對(duì)大家的學(xué)習(xí)有所啟發(fā),幫助大家更好地學(xué)習(xí)Android軟件編程。

相關(guān)文章

  • Kotlin函數(shù)使用示例教程

    Kotlin函數(shù)使用示例教程

    這篇文章主要為大家介紹了Kotlin函數(shù)的使用示例教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Android中Market的Loading效果實(shí)現(xiàn)方法

    Android中Market的Loading效果實(shí)現(xiàn)方法

    這篇文章主要介紹了Android中Market的Loading效果實(shí)現(xiàn)方法,較為詳細(xì)的分析了Android中l(wèi)oading效果的相關(guān)布局及功能實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • Android實(shí)現(xiàn)手勢(shì)滑動(dòng)識(shí)別功能

    Android實(shí)現(xiàn)手勢(shì)滑動(dòng)識(shí)別功能

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)手勢(shì)滑動(dòng)識(shí)別功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Android解析XML文件升級(jí)APK的方法

    Android解析XML文件升級(jí)APK的方法

    這篇文章主要介紹了Android解析XML文件升級(jí)APK的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-07-07
  • AlertDialog點(diǎn)擊按鈕不消失的實(shí)現(xiàn)方法

    AlertDialog點(diǎn)擊按鈕不消失的實(shí)現(xiàn)方法

    我有一個(gè)文本輸入對(duì)話框,當(dāng)我點(diǎn)擊對(duì)話框上的“是”按鈕,它會(huì)驗(yàn)證輸入,然后關(guān)閉對(duì)話框,但是,如果輸入錯(cuò)誤,我想停留在同一個(gè)對(duì)話框中。怎么實(shí)現(xiàn)此功能呢?下面通過(guò)本文給大家分享下
    2017-01-01
  • Android 自定義彈性ListView控件實(shí)例代碼(三種方法)

    Android 自定義彈性ListView控件實(shí)例代碼(三種方法)

    關(guān)于在Android中實(shí)現(xiàn)ListView的彈性效果,有很多不同的方法,網(wǎng)上一搜,也有很多,下面貼出在項(xiàng)目中經(jīng)常用到的兩種實(shí)現(xiàn)ListView彈性效果的方法(基本上拿來(lái)就可以用),需要的朋友參考下本段代碼
    2016-01-01
  • Android利用Xfermode剪裁圓角

    Android利用Xfermode剪裁圓角

    這篇文章主要為大家詳細(xì)介紹了Android利用Xfermode剪裁圓角,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android 顯示刷新頻率的實(shí)現(xiàn)代碼

    Android 顯示刷新頻率的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Android 顯示刷新頻率的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • Android中的webview監(jiān)聽(tīng)每次URL變化實(shí)例

    Android中的webview監(jiān)聽(tīng)每次URL變化實(shí)例

    這篇文章主要介紹了Android中的webview監(jiān)聽(tīng)每次URL變化實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • Android在類微信程序中實(shí)現(xiàn)藍(lán)牙聊天功能的示例代碼

    Android在類微信程序中實(shí)現(xiàn)藍(lán)牙聊天功能的示例代碼

    這篇文章主要介紹了Android在類微信程序中實(shí)現(xiàn)藍(lán)牙聊天功能,本文通過(guò)實(shí)例代碼給大家介紹的非常想詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06

最新評(píng)論