android 显示notification

    xiaoxiao2023-03-24  5

    MainActivity.java

    package com.example.tom.notification; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button b1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onNoti(View view){ // 在Android进行通知处理,首先需要重系统哪里获得通知管理器NotificationManager,它是一个系统Service。 NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); PendingIntent pendingIntent3 = PendingIntent.getActivity(this, 0, new Intent(this, NotificationApp.class), 0); // 通过Notification.Builder来创建通知,注意API Level // API16之后才支持 Notification notify3 = null; // 需要注意build()是在API if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { notify3 = new Notification.Builder(this) .setSmallIcon(R.drawable.star) .setTicker("TickerText:" + "您有新短消息,请注意查收!") .setContentTitle("Notification Title") .setContentText("This is the notification message") .setContentIntent(pendingIntent3).setNumber(1).build(); } // level16及之后增加的,API11可以使用getNotificatin()来替代 notify3.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。 manager.notify(11, notify3);// 步骤4:通过通知管理器来发起通知。如果id不同,则每click,在status哪里增加一个提示 } } NotificationApp.java

    package com.example.tom.notification; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class NotificationApp extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); } } androidManifest.xml

    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.tom.notification"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".NotificationApp" android:label="Details of notification" android:parentActivityName=".MainActivity" > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity"></meta-data> </activity> </application> </manifest> activity_main.xml

    <?xml version="1.0" encoding="utf-8"?> <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="com.example.tom.notification.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Notification" android:id="@+id/button" android:onClick="onNoti" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="124dp" /> </RelativeLayout>

    activity_notification.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"> <TextView android:layout_width="fill_parent" android:layout_height="400dp" android:text="Hi, Your Detailed notificaiton view goes here..." /> </LinearLayout> drawable 文件夹中添加一张名为star.png的图片

    转载请注明原文地址: https://ju.6miu.com/read-1202476.html
    最新回复(0)