MainActivity
package com.example.administrator.myapplication;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.os.Build;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button normalToast, imageToast;
private Button createNotify, deleteNotify;
private int NOTIFICATION_ID = 0x123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
normalToast = (Button) findViewById(R.id.normalToast);
imageToast = (Button) findViewById(R.id.imageToast);
createNotify = (Button) findViewById(R.id.createNotify);
deleteNotify = (Button) findViewById(R.id.deleteNotify);
// 显示普通文本Toast
normalToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "普通的Toast信息", Toast.LENGTH_SHORT).show();
}
});
// 显示自定义视图Toast
imageToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast toast = Toast.makeText(MainActivity.this, "带图片的Toast信息", Toast.LENGTH_SHORT);
View toastView = toast.getView();
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.mipmap.ic_launcher);
LinearLayout linearLayout = new LinearLayout(MainActivity.this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.addView(imageView);
linearLayout.addView(toastView);
toast.setView(linearLayout);
toast.show();
}
});
createNotify.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View v) {
Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setSmallIcon(R.mipmap.ic_launcher_round);
builder.setTicker("Notification的提示测试");
builder.setContentTitle("Notification的标题测试");
builder.setContentText("Notification的内容测试");
builder.setWhen(System.currentTimeMillis());
Notification notification = builder.build();
notification.defaults = Notification.DEFAULT_SOUND;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
}
});
deleteNotify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationManager.cancel (NOTIFICATION_ID);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.example.administrator.myapplication.MainActivity">
<Button
android:id="@+id/normalToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通Toast"/>
<Button
android:id="@+id/imageToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="带图片Toast"/>
<Button
android:id="@+id/createNotify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="创建消息"/>
<Button
android:id="@+id/deleteNotify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除消息"/>
</LinearLayout>