Notification通知栏
首先实现的功能就是通知栏显示Notification,Notification是显示在系统的通知栏上面的,所以Notification
是属于进程之前的通讯。进程之间的通讯是要在系统中获取系统的服务的。
1.NotificationManager nm=(NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
接下就是把Notification类用NotificationManager来管理。
2.nm.notify(0, notification);
然后Notification还是设置内容,标题、图标、内容、跳转。
代码功能:实现Notification中的通知栏打电话给110,别忘了Manifest.xml中的权限
<uses-permission android:name="android.permission.CALL_PHONE"/>
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view){
NotificationManager nm=(NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
Notification notification=new Notification(R.drawable.ic_launcher, "我是一条消息", System.currentTimeMillis());
notification.flags=Notification.FLAG_AUTO_CANCEL;
Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:110"));
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "标题", "内容", contentIntent);
nm.notify(0, notification);
}
}
XML中的文件
activity_main.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: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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:onClick="click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:layout_centerInParent="true"
android:text="点击显示通知栏"
/>
</RelativeLayout>