日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android notification应用之自定义来电通知

發布時間:2024/1/8 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.下面來實現廣播接受者

public class NotificationBroadcast extends BroadcastReceiver {private final static String TAG = "NotificationBroadcast";@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();//動作Log.e(TAG, "廣播 Action = " + action);if (action.equals("action.accept")) {Log.e(TAG, "接通電話");Call call = SiphoneService.getCore().getCurrentCall();if(call != null){Call.State state = call.getState();if(state.equals(Call.State.IncomingEarlyMedia) ||state.equals(Call.State.IncomingReceived)){if(SiphoneService.manager!=null){System.out.println("call notificationId1:"+SiphoneService.notificationId);SiphoneService.manager.cancel(SiphoneService.notificationId);}CallParams params = SiphoneService.getCore().createCallParams(call);call.acceptWithParams( params );}}}else if (action.equals("action.decline")) {Log.e(TAG, "掛斷電話");Call call = SiphoneService.getCore().getCurrentCall();if(call != null){Call.State state = call.getState();if(state.equals(Call.State.IncomingEarlyMedia) ||state.equals(Call.State.IncomingReceived)){//這里的manager是前面代碼中的關于notification的manager 掛斷電話既是對這條通知的處理 接通電話同理 就直接取消掉前面的通知if(SiphoneService.manager!=null){System.out.println("call notificationId1:"+SiphoneService.notificationId);SiphoneService.manager.cancel(SiphoneService.notificationId);}call.terminate();}}}} //這里我沒用這個方法對通知欄進行伸縮 因為當我選擇掛斷電話還是接聽電話 都對這個消息進行了處理 直接使用了上面的取消通知的功能 // public void collapseStatusBar(Context context) { // try { // Object statusBarManager = context.getSystemService("statusbar"); // Method collapse; // if (Build.VERSION.SDK_INT <= 16) { // collapse = statusBarManager.getClass().getMethod("collapse"); // } else { // collapse = statusBarManager.getClass().getMethod("collapsePanels"); // } // collapse.invoke(statusBarManager); // } catch (Exception localException) { // localException.printStackTrace(); // }}

廣播是我們自己定義的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应用之自定义来电通知的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。