posts - 11,comments - 0,views - 6303

 

 

 

实现通知及跳转:

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" );
    }

 

 

 

 下拉菜单

 

 

 

 

 

 

 

posted on   初窗景  阅读(189)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示