1.Toast
1.1.布局activity_main.xml
1.2.Java代码 MainActivity.java<?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" 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.rj141.sb.toastdemo.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是一个时间很短的消息提示" android:id="@+id/shortToast" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是一个时间较长的消息提示" android:id="@+id/longToast" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是一张图片的消息提示" android:id="@+id/imageToast" /> </LinearLayout>
1.3.效果图public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button shortToast,longToast,imageToast; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); shortToast= (Button) this.findViewById(R.id.shortToast); longToast= (Button) this.findViewById(R.id.longToast); imageToast= (Button) this.findViewById(R.id.imageToast); shortToast.setOnClickListener(this); longToast.setOnClickListener(this); imageToast.setOnClickListener(this); } @Override public void onClick(View v) { int id=v.getId(); switch (id){ case R.id.shortToast: Toast shorttoast=Toast.makeText(MainActivity.this,"这是一个时间很短的消息提示",Toast.LENGTH_SHORT); //定义消息显示的位置 shorttoast.setGravity(Gravity.CENTER, 0, 0); shorttoast.show(); break; case R.id.longToast: Toast.makeText(MainActivity.this,"这是一个时间较长的消息提示",Toast.LENGTH_LONG).show(); break; case R.id.imageToast: Toast image=Toast.makeText(MainActivity.this, "这是一张图片的消息提示", Toast.LENGTH_LONG); ImageView imageView=new ImageView(MainActivity.this); imageView.setImageResource(R.drawable.garden07); image.setView(imageView); image.setGravity(Gravity.CENTER,0,300); image.show(); break; } } }
2.Notification
2.1.布局
2.2.MainActivity<?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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.rj141.sb.notificationdemo.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="创建一个通知" android:id="@+id/btn" /> </LinearLayout>
Notification notification=builder.build(); NotificationManager manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(2002,notification); } }); }} 2.3.效果图public class MainActivity extends AppCompatActivity { private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn= (Button) this.findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { NotificationCompat.Builder builder=new NotificationCompat.Builder(MainActivity.this); builder.setSmallIcon(R.drawable.ic_launcher); builder.setContentTitle("通知"); builder.setContentText("一个简单的通知"); builder.setContentInfo("简单的通知,特别简单的创建"); <pre name="code" class="java"> //设置声音 Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); builder.setSound(uri);
详解Notification
1.创建一个通知栏的Builder构造类 (Create a Notification Builder)
1.实例化通知栏构造器NotificationCompat.Builder
2.对Builder进行配置,图片、声音、标题、内容等
3.获取状态通知栏管理:
NotificationManager manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)
2.定义通知栏的Action (Define the Notification's Action)
3.设置通知栏点击事件 (Set the Notification's Click Behavior)
设置通知栏PendingIntent(点击动作事件等都包含在这里)
PendingIntent的位标识符:
FLAG_ONE_SHOT 表示返回的PendingIntent仅能执行一次,执行完后自动取消
FLAG_NO_CREATE 表示如果描述的PendingIntent不存在,并不创建相应的PendingIntent,而是返回NULL
FLAG_CANCEL_CURRENT 表示相应的PendingIntent已经存在,则取消前者,然后创建新的PendingIntent,这个有利于数据保持为最新的,可以用于即时通信的通信场景
FLAG_UPDATE_CURRENT 表示更新的PendingIntent
4.通知 (Issue the Notification)