android notification应用之自定义来电通知
生活随笔
收集整理的這篇文章主要介紹了
android notification应用之自定义来电通知
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
android notification應用之自定義來電通知
1.為了實現老板的各種要求 本人矜矜業業完成任務 隨著這個軟電話軟件的日益完善 本來來電的時候是創建一條通知點亮屏幕 用戶可以解鎖屏幕后接起電話 但老板又覺得體驗不好 想直接在鎖屏狀態下直接接起電話 我就尋思著 自帶的notification通知欄樣式肯定是不能實現這個接電話掛電話的功能的 那就相方設法自己寫一個通知欄樣式唄 我本來以為自定義通知欄不好實現 加上還有按鈕的相關接電話和掛電話的功能 (ps:這個時候我請教了我的老師 其實非常感謝他的幫助 讓我在四五個月內 學了不少android的開發技能 問他問題他會給我講解大致思路 然后讓我自己按照這個思路去完成要求 實在不會了 他也會給我提供具體的方法 總之就是很感謝那個大哥哥了)
2. 自定義通知的實現主要是用到了RemoteViews來對通知欄樣式進行更改 ,本來通知欄的創建使用了 Notification.Builder(this, CHANNEL_ONE_ID) 自定義通知欄時使用了NotificationCompat.Builder(this) 下面來貼一下代碼 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)private void showNotification(String phoneNum) {if (phoneNums.size()>0) {notificationId = phoneNums.indexOf(phoneNum);} else{notificationId = 100;}manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);String CHANNEL_ONE_ID = getPackageName();String CHANNEL_ONE_NAME = "Channel One";NotificationChannel notificationChannel = null;manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {notificationChannel = new NotificationChannel(CHANNEL_ONE_ID, CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH);notificationChannel.enableLights(true);notificationChannel.setLightColor(Color.RED);notificationChannel.setShowBadge(true);notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);manager.createNotificationChannel(notificationChannel);}NotificationCompat.Builder builder = new NotificationCompat.Builder(this);builder.setSmallIcon(R.mipmap.ic_launcher);//使用RemoteViews時,設置的是狀態欄中的小圖標,必須要設置builder.setAutoCancel(true);//設置是否點擊通知后會自動消失Notification notification = builder.build();//通過xml創建RemoteViews,并且動態改變布局中的內容RemoteViews views = new RemoteViews(getPackageName(), R.layout.layout_notification);views.setTextViewText(R.id.notification_title, "WES");views.setTextViewText(R.id.notification_content, Sys.details);Date date = new Date();SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");//按鈕點擊事件://接通電話的相關操作PendingIntent acceptIntent = PendingIntent.getBroadcast(this,notificationId ,new Intent("action.accept"),PendingIntent.FLAG_UPDATE_CURRENT);views.setOnClickPendingIntent(R.id.accept_icon,acceptIntent);//點擊的id,點擊事件/掛斷電話的相關操作(點擊掛斷電話的按鈕會發送一個action為action.decline的廣播,然后廣接收者受到廣播對廣播進行處理 )PendingIntent declineIntent = PendingIntent.getBroadcast(this,notificationId ,new Intent("action.decline"),PendingIntent.FLAG_UPDATE_CURRENT);views.setOnClickPendingIntent(R.id.decline_icon,declineIntent);//點擊的id,點擊事件String time = sdf.format(date);views.setTextViewText(R.id.notification_time, time);//這里需要注意,如果不設置 notification.bigContentView ,則由于通知的高度是固定的,如果remoteview的布局超過了其通知的高度,//就會有一部分顯示不出來了notification.bigContentView = views;notification.contentView = views;notification.visibility = Notification.VISIBILITY_PUBLIC;//給整個通知設置一個拉起來電頁面的PendingIntentIntent intentNotification = new Intent(SiphoneService.this, CallIncomingActivity.class);intentNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intentNotification.setPackage("com.sux.sip");PendingIntent pi = PendingIntent.getActivity(this, notificationId, intentNotification, PendingIntent.FLAG_CANCEL_CURRENT);notification.contentIntent = pi;//單獨給RemoteView中的控件設置PengdingIntent // Intent intentNotification2 = new Intent(); // intentNotification2.setComponent(new ComponentName("com.netease.newsreader.activity","com.netease.nr.biz.ad.AdActivity")); // intentNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // PendingIntent pi2 = PendingIntent.getActivity(context, 123, intentNotification2, PendingIntent.FLAG_CANCEL_CURRENT); // views.setOnClickPendingIntent(R.id.iv_banner,pi2);manager.notify(notificationId, notification);}
這里我給相同的電話號碼設置了相同的notificationId,這樣通知的時候就只會出現一條通知 還有一點沒完善 應該要填上來電幾條的 嗯 以后再加上吧
3.下面來實現廣播接受者
廣播是我們自己定義的action 所以在AndroidManifest.xml中需要攔截一下廣播的action
<receiverandroid:name=".receviers.NotificationBroadcast"android:enabled="true"><intent-filter><action android:name="action.accept"/><action android:name="action.decline"/></intent-filter></receiver>4.貼一下我自定義的notification布局 layout_notification.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="5dp"><TextViewandroid:id="@+id/notification_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="WES"/><TextViewandroid:id="@+id/notification_time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:text="23:34"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="5dp"><TextViewandroid:id="@+id/notification_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="111111111111111"/></LinearLayout><LinearLayoutandroid:id="@+id/acceptUnlock"android:layout_width="match_parent"android:layout_height="40dp"android:orientation="horizontal"><ImageViewandroid:id="@+id/accept_icon"android:src="@drawable/call_start"android:layout_width="match_parent"android:layout_height="match_parent"android:padding="3dp"android:layout_weight="1"/><ImageViewandroid:id="@+id/decline_icon"android:src="@drawable/hangup"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"/></LinearLayout> </LinearLayout>5.總結一下 貼代碼真的很爽 寫起文字來真的很煩
最后實現的結果就是在鎖屏下會有一條通知點亮屏幕 通知的內容是來電的相關信息 可以選擇接電話和掛斷電話 接電話后居然可以跨過鎖屏來到通話界面 點擊縮放可以使通話界面懸浮 再次進到通話界面需要解鎖 解鎖后也有懸浮窗也可以回到通話界面
總結
以上是生活随笔為你收集整理的android notification应用之自定义来电通知的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 长沙理工大学ACMore编程协会2018
- 下一篇: “减糖”迫在眉睫,“代糖”或成最佳考量?