当前位置:网站首页>Notification is the notification displayed in the status bar of the phone

Notification is the notification displayed in the status bar of the phone

2022-07-07 17:37:00 XLMN

notification Is the notification displayed in the phone status bar , The mobile status bar is located at the top of the mobile , Generally, it displays the current network status of the mobile phone , Battery status , Actual, etc ,notification It represents a kind of notification with global effect

package com.example.check;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
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.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends Activity {

      static final int NOTIFICATION_ID = 0x123;
        private NotificationManager nm;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.notification);
            // Get system's NotificationManager service 

            nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        }

        // Define the processing method for the button click event that sends the notification 
        public void send(View source) {
            // Create one to start other activity Of intent
            Intent it = new Intent(MainActivity.this, OtherActivity.class);
            PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, it, 0);
            Notification nf = new Notification.Builder(this)
                    // Set open change notification , The notification disappears automatically 
                    .setAutoCancel(true)
                    // Set the notification prompt displayed in the status bar 
                    .setTicker(" You have new news ")
                    // Set the icon for the notification 
                    .setSmallIcon(R.drawable.mia5)
                    // Set the title of the notification 
                    .setContentTitle(" A new notice ")
                    // Set notification content 
                    .setContentText(" Congratulations , You were accepted by Huangshan University , The major of Chinese language and literature was admitted ")
                    // Set to use the system default sound , default led The lamp 
                    .setDefaults(Notification.DEFAULT_SOUND)
                    // Set a custom sound for notifications  .setSound(Uri.parse("android.resource://org.crazyit.ui/"+R.raw.msg))
                  // .setWhen(System.currentTimeMillis())
                    // Set the notification that the program is about to start intent
                    .setContentIntent(pi)
                    .build();
            // Sending notice 
            nm.notify(NOTIFICATION_ID, nf);
        }

        // Define the event handling method for the click event of the button to delete the notification 
        public void del(View V) {
            // Notice of cancellation 
            nm.cancel(NOTIFICATION_ID);
        }
    }

package com.example.check;

import android.app.Activity;
import android.os.Bundle;

public class OtherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
}
}

<?xml version="1.0" encoding="utf-8"?>



<?xml version="1.0" encoding="utf-8"?>



![ Insert picture description here ](https://img-blog.csdnimg.cn/4f9b251331ac4be6adc686fae39e14e0.png) ![ Insert picture description here ](https://img-blog.csdnimg.cn/93673e88abb34f429ee563743589fabe.png) ![ Insert picture description here ](https://img-blog.csdnimg.cn/52f5afeff47846839126680e28ce79bc.png)
原网站

版权声明
本文为[XLMN]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071527553452.html