首页 > 其他分享 >Notification

Notification

时间:2022-09-18 15:36:19浏览次数:59  
标签:Notification void savedInstanceState private manager onCreate

 

 

 

实现通知及跳转:

layout中activity.xml中代码:

 <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="sendNotification"
        android:text="发出通知"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="cacelNotification"
        android:text="取消通知"/>

 

 

Mainactivity中:

public class MainActivity extends AppCompatActivity {
    private NotificationManager manager;
    private Object Notification;
    private Notification notification;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
         if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
             NotificationChannel channel = new NotificationChannel("leo", "测试通知",
                     NotificationManager.IMPORTANCE_HIGH);
             manager.createNotificationChannel(channel);
         }
       Intent intent =  new Intent(this,NotificationActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        notification = new NotificationCompat.Builder(this,"leo")
                .setContentTitle("官方通知")
                .setContentText("世界和平")
                .setSmallIcon(R.drawable.ic_baseline_battery_full_24)
                .setColor(Color.parseColor("#ff0000"))
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .build();
    }
    public void sendNotification(View view) {
       
    }
    public void cacelNotification(View view){
       
    }

新建class

protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("leo", "进入NotificationActivity" );
    }

 

 

 

 下拉菜单

 

 

 

 

 

 

 

标签:Notification,void,savedInstanceState,private,manager,onCreate
From: https://www.cnblogs.com/ccwj/p/16704852.html

相关文章

  • NotificationService.appex编译报错
    报错:Buildinputfilecannotbefound:'.../Build/Products/Debug-iphoneos/NotificationService.appex/NotificationService' 解决的办法:1、选择扩展Targets-Bui......