Android开发:三步实现点击Notification触发Activity跳转
在Android开发中,点击notification触发activity跳转,可以通过以下步骤实现: 1. 创建一个BroadcastReceiver,用于接收点击notification的Intent。 ```java public class NotificationReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // 在这里处理点击notification的逻辑 // 例如,启动一个新的Activity Intent intentToStart = new Intent(context, MainActivity.class); context.startActivity(intentToStart); } } ``` 2. 在AndroidManifest.xml文件中注册这个BroadcastReceiver。 ```xml <receiver android:name=".NotificationReceiver"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </receiver> ``` 3. 在创建notification的时候,设置点击notification的Intent。 ```java Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setContentTitle("Notification Title") .setContentText("Notification Content") .setSmallIcon(R.drawable.notification_icon) .setContentIntent(pendingIntent); ``` 这样,当用户点击notification的时候,就会启动MainActivity。 ######[AI写代码神器 | 268点数解答 | 2023-11-07 22:42:43]
- RealtimeMonitoringFragment 代码片段:Android 实时监控功能核心代码剖析(字节豆包 | 30点数解答 | 2024-10-12 10:31:18)294
- 揭秘:Android实时监控片段(RealTimeMonitoringFragment)代码实现(GPT | 531点数解答 | 2024-10-12 10:32:25)269
- AIDE编写Xposed软件:参考代码教你拦截Smail类方法名(阿里通义 | 649点数解答 | 2024-10-12 00:15:48)305
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)345
- iOS开发必知:delegate与notification的四大区别及应用场景解析(阿里通义 | 209点数解答 | 2023-11-08 00:40:02)205
- 智能电话窗口按钮1弹起:获取坐标与手机IMEI码待处理(GPT | 208点数解答 | 2024-09-12 16:36:31)353
- 易语言携手大漠插件:多窗口多线程单控脚本完整实现,含信息获取与模拟按键功能!(DeepSeek | 1541点数解答 | 2025-08-01 07:14:07)174
- 易语言+大漠插件:多窗口多线程单控脚本实现,含信息获取与模拟按键功能(字节豆包 | 949点数解答 | 2025-08-01 07:15:59)172
- 易语言结合大漠插件:多窗口多线程单控脚本开发全解析(阿里通义 | 1131点数解答 | 2025-08-01 07:16:58)165
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)313
- Android开发:用Intent轻松实现Activity切换与数据传递(阿里通义 | 308点数解答 | 2023-11-07 22:46:17)307
- 用AIDE编写Xposed模块:精准拦截指定Smali代码调用(GPT | 1220点数解答 | 2024-11-01 21:38:25)187