remoteViewsによるAndroidのカスタム通知

免責事項:この記事はブロガーオリジナル記事です、続くBY-SAのCC 4.0を著作権契約、複製、元のソースのリンクと、この文を添付してください。
このリンク: https://blog.csdn.net/weixin_43219615/article/details/100046209

1.はじめに

私たちは、remoteViewsでプッシュ通知をカスタマイズし、多くの制限があるremoteViewsことができます。remoteViewsのみ、以下をサポートしてウィジェット:AnalogClock、ボタン、クロノメーター、のImageButton ;、ImageViewの、プログレスバー、TextClock、TextViewには:AdapterViewFlipper、でframeLayout、GridLayoutの、GridViewの唯一の次のレイアウトをサポート
、のLinearLayout、リストビュー、RelativeLayout、StackView
、ViewFlipperを。
絵
絵
主な方法:
setViewVisibility:設定はコントロールが表示されているかどうかを指定します。
setViewPadding:指定されたコントロールのピッチを設定します。
setTextViewText:設定は、TextViewのか、Buttonコントロールのテキストを指定します。
setTextViewTextSize:設定は、TextViewのか、Buttonコントロールのテキストのサイズを指定します。
SETTEXTCOLOR:TextViewのか、Buttonコントロールを指定したテキストの色を設定します。
setTextViewCompoundDrawables:設定は、周囲のテキストアイコンのTextViewにまたはボタンコントロールを指定します。
setImageViewResource:ImageViewのかImgaeButton制御リソース数を設定します。
setImageViewBitmap:ビットマップオブジェクトのImageViewのかImgaeButtonコントロールを設定します。
setChronometer:タイマー情報を設定します。
setProgressBar:プログレスバーが最大値と現在の進行状況などの情報を提供しました。
setOnClickPendingIntent:[設定]指定された制御応答アクションをクリックします。
工事完了後のオブジェクトを呼び出すと、設定RemoteViewsはのsetContentメソッドをオブジェクト、カスタム通知定義を完了することへの通知。

2.例

  1. カスタムレイアウトファイルの通知。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minHeight="60dp"
    >

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:scaleType="fitCenter"
        android:src="@drawable/tt"/>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:orientation="vertical"
        >

        <ProgressBar
            android:id="@+id/pb_play"
            android:layout_width="match_parent"
            style="?android:progressBarStyleHorizontal"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:max="100"
            android:progress="10"/>

        <TextView
            android:id="@+id/tv_play"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:textColor="@color/black"
            android:textSize="17sp"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <Chronometer
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:id="@+id/ch_play"/>

        <Button
            android:id="@+id/btn_play"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:text="暂停"
            android:textColor="@color/black"
            android:textSize="17sp"/>
    </LinearLayout>
</LinearLayout>
  1. カスタム通知コードを使用してください。
private void sendCustomNotification(Context context, String song, boolean isPlay, int progress, long time) {
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, R.string.app_name, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        RemoteViews remoteViews = new RemoteViews(this.getPackageName(), R.layout.remote_view_demo);

        if(isPlay) {
            remoteViews.setTextViewText(R.id.tv_play, "正在播放" + song);
            remoteViews.setTextViewText(R.id.btn_play, "暂停");
            remoteViews.setChronometer(R.id.ch_play, time, "%s", true);
        } else {
            remoteViews.setTextViewText(R.id.tv_play, "暂停播放" + song);
            remoteViews.setTextViewText(R.id.btn_play, "播放");
            remoteViews.setChronometer(R.id.ch_play, time, "%s", false);
        }

        remoteViews.setProgressBar(R.id.pb_play, 100, progress, false);
        remoteViews.setOnClickPendingIntent(R.id.btn_play, pendingIntent);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "message");
        builder.setContentIntent(pendingIntent).setContent(remoteViews).setTicker(song).setSmallIcon(R.drawable.tt_s);
        Notification notification = builder.build();
        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, notification);
    }

おすすめ

転載: blog.csdn.net/weixin_43219615/article/details/100046209