安卓手機使用NotificationManager彈出消息框,在上拉工具欄中

首先放上效果圖給大家看

在手機的上方彈出消息或提示

我這裏使用一個簡單的按鈕來彈一個消息欄

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="發送通知"
        android:onClick="send"
        />

在onCreate方法裏獲取通知管理器

//獲取通知管理器
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

然後在java代碼中寫下點擊事件

    public void send(View view){
        //首先實例消息通知
        NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
        //設置標題
        builder.setContentTitle("提示");
        //設置內容
        builder.setContentText("美女,暗戀你好久了");
        //設置圖標
        builder.setSmallIcon(android.R.drawable.star_on);
        //默認的閃光燈、鈴聲等等,一切默認爲主
        builder.setDefaults(NotificationCompat.DEFAULT_ALL);

        Notification notification=builder.build();


        //發送通知
        notificationManager.notify(0x101,notification);



    }

然後效果就可以出來了,安卓手機有些可能有權限的問題,我實在模擬器上實現的,我的手機是OPPOA37,沒有出來消息框
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章