今天学习并測试了Notification组件,这个组件在应用中也经经常使用到。在这里写了一个简单的Demo。
Notification是显示在状态栏的消息----位于手机屏幕的最上方。
程序一般通过NotificationManager服务来发送Notification。
Notification发送Notification的步骤
1、调用getSystemService(NOTIFICATION_SERVICE)方法获取系统
NotificationManager服务
2、通过构造器创建一个Notification对象
3、为Notification设置各种属性
4、通过NotificationManerger发送Notification
在AndroidManifest.xml文件里设置权限:
<!-- 设置闪光灯权限--> <uses-permission android:name="android.permission.FLASHLIGHT"></uses-permission> <!--设置震动权限--> <uses-permission android:name="ANDROID.PERMISSION.VIBRATE"></uses-permission>
xml文件:
<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: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=".MainActivity"> <Button android:id="@+id/startNotify" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启通知" /> <Button android:id="@+id/stopNotify" android:layout_width="wrap_content" android:layout_toRightOf="@+id/startNotify" android:layout_height="wrap_content" android:text="关闭通知"/> </RelativeLayout>MainActivity.java
package lzl.edu.com.notificationtest; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener{ private static final int NOTIFICATION = 1; Button startNotify,stopNotify; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startNotify =(Button)findViewById(R.id.startNotify); stopNotify=(Button)findViewById(R.id.stopNotify); startNotify.setOnClickListener(this); stopNotify.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.startNotify: Intent intent = new Intent(MainActivity.this,NextActivity.class); PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); //创建一个Notification Notification notification = new Notification(); //为Notification设置图标 notification.icon = R.mipmap.ic_launcher; //设置文本内容 notification.tickerText="启动还有一个应用的通知"; //设置发送年时间 notification.when = System.currentTimeMillis(); //设置声音 notification.defaults = notification.DEFAULT_SOUND; //设置默认声音、震动、闪光灯 notification.defaults=notification.DEFAULT_ALL; notification.setLatestEventInfo(MainActivity.this,"一条应用的通知","点击查看",pi); //获取系统的NotificationManerger服务 NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(NOTIFICATION,notification); break; case R.id.stopNotify: NotificationManager notificationManager1 = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); notificationManager1.cancel(NOTIFICATION); break; default:break; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }