Android-Notification 简单介绍



public class MainActivity extends AppCompatActivity {

    private NotificationManager mNotificationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mNotificationManager = (NotificationManager)getSystemService(MainActivity.NOTIFICATION_SERVICE);

                String tickerText1 = "this is notification";

                int icon1 = android.R.drawable.btn_plus;

                Intent notificationIntent1 = new Intent(MainActivity.this, MainActivity.class);
                notificationIntent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                PendingIntent contentIntent1 = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent1, 0);

                Notification notification1 = new Notification(icon1, tickerText1, System.currentTimeMillis());

                notification1.defaults |= Notification.DEFAULT_SOUND;

                notification1.defaults |= Notification.DEFAULT_VIBRATE;
                notification1.flags|=Notification.FLAG_ONGOING_EVENT;

                RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.remoteview_layout);
                contentView.setTextViewText(R.id.notificationTitle, "Downloading...");
                contentView.setTextViewText(R.id.notificationPercent, "35%");
                contentView.setProgressBar(R.id.notificationProgress, 100, 35, false);

                notification1.contentView = contentView;
                notification1.contentIntent=contentIntent1;

                mNotificationManager.notify(0, notification1);
            }
        });
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.cootek.rvtest.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送notification"
        android:id="@+id/button"
        android:layout_below="@+id/textView"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="218dp" />
</RelativeLayout>

remoteview_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="3dp"
    >
    <ImageView android:id="@+id/notificationImage"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:src="@drawable/icon_dialer"
        />
    <TextView android:id="@+id/notificationTitle"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_toRightOf="@id/notificationImage"
        android:layout_alignParentRight="true"
        android:paddingLeft="6dp"
        android:textColor="#FF000000"
        />
    <TextView android:id="@+id/notificationPercent"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_below="@id/notificationImage"
        android:paddingTop="2dp"
        android:textColor="#FF000000"
        />
</RelativeLayout>


发布了120 篇原创文章 · 获赞 25 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/yearningseeker/article/details/52276726