在android中有时会在主界面上收到某些应用的推送,有的可以包含图片,声音或者震动效果,当点击这些提示时,有时还可以进入到发送提示的的应用。
这些提示的推送就是通知,当然通知早根本上也是你一种服务。
首先想要使用通知就必须使用到Notification.Builder 和NotificationManager这两个类
使用Notification.Builder来取得Notification对象,使用NotificationManager来取得操作通知的对象
当然,我们也可以使用RemoteViews来获得自定义View来创建自定义的通知
接下来就让我们一起用例子来学习通知的使用吧
主界面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" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="15dp" android:text="原始通知" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="53dp" android:text="自定通知" /> </RelativeLayout>
通知界面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" 主界面哦" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout>
自定义通知界面
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:src="@drawable/b12" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/imageView1" android:text="大学橙色预警" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_toRightOf="@+id/imageView1" android:text="部分地区降雨达到500ml" /> </RelativeLayout>
主文件
package com.example.notification1; 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.View; import android.widget.Button; import android.widget.RemoteViews; public class MainActivity extends Activity { private Button button1, button2; private Notification.Builder builder; private NotificationManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button) this.findViewById(R.id.button1); button2 = (Button) this.findViewById(R.id.button2); builder = new Notification.Builder(this);// 创建一个通知设置 manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub PendingIntent pendingIntent = PendingIntent.getActivity( MainActivity.this, 0, new Intent(MainActivity.this, Main.class), 0);// 获得pendingIntent意图 builder.setContentIntent(pendingIntent); builder.setSmallIcon(R.drawable.b12); builder.setTicker("您有新通知"); builder.setContentTitle("降雨橙色预警"); builder.setContentText("下午5时降雨将会达到500ml,请市民注意防范"); builder.setContentInfo("暴雨,大风"); builder.setSubText("本月底7次大雨"); builder.setDefaults(Notification.DEFAULT_SOUND);// 设置默认提示生啊 long[] vibrate = { 100l, 1000l, 100l, 1000l, 10l, 1000l };// 设置震动的频率 builder.setVibrate(vibrate);// 设置自定义震动 Notification notification = builder.build(); manager.notify(1000, notification);// 开启通知 } }); //自定义通知 button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub RemoteViews contentRemoteViews = new RemoteViews( getPackageName(), R.layout.custom);// 获取自定义的layout Intent intent = new Intent(MainActivity.this, Main.class); PendingIntent conPendingIntent = PendingIntent.getActivity( MainActivity.this, 0, intent, 0); builder.setContent(contentRemoteViews); builder.setContentIntent(conPendingIntent); Notification notification = builder.build(); manager.notify(1001, notification); } }); } }
通知界面JAVA文件
package com.example.notification1; import android.app.Activity; import android.os.Bundle; public class Main extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); super.setContentView(R.layout.main); } }
点击通知
点击自定义通知
通过本节的学习,希望读者能够掌握原始通知的使用和自定义通知的配置和使用。
下节预报:BorderCast广播
时间: 2024-10-05 08:12:33