Android中通知Notification使用實例(振動、燈光、聲音)
本文實例講解了通知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 viewpager自動輪播和小圓點聯(lián)動效果
這篇文章主要為大家詳細介紹了Android viewpager自動輪播和小圓點聯(lián)動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10Android系統(tǒng)中使用shareuserid獲取系統(tǒng)權限的教程
這篇文章主要介紹了Android系統(tǒng)中使用shareuserid獲取系統(tǒng)權限的教程,這樣以來不同的apk就可以互相訪問對應的app文件夾,需要的朋友可以參考下2016-04-04Android OKHttp框架的分發(fā)器與攔截器源碼刨析
okhttp是一個第三方類庫,用于android中請求網(wǎng)絡。這是一個開源項目,是安卓端最火熱的輕量級框架,由移動支付Square公司貢獻(該公司還貢獻了Picasso和LeakCanary) 。用于替代HttpUrlConnection和Apache HttpClient2022-11-11Android AIDL中Map參數(shù)傳遞的問題詳解
這篇文章主要給大家介紹了關于Android AIDL中Map參數(shù)傳遞問題的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友下面來一起看看吧。2017-12-12Kotlin文件讀寫與SharedPreferences存儲功能實現(xiàn)方法
SharedPreferences是安卓平臺上一個輕量級的存儲類,用來保存應用的一些常用配置,比如Activity狀態(tài),Activity暫停時,將此activity的狀態(tài)保存到SharedPereferences中;當Activity重載,系統(tǒng)回調(diào)方法onSaveInstanceState時,再從SharedPreferences中將值取出2022-12-12