实现通知及跳转:
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