PendingIntent android.app.PendingIntent.getActivity的使用

本文介绍了如何使用Android的PendingIntent.getActivity方法来启动一个新的Activity。该方法允许开发者在非活动上下文中启动新的Activity,并强调了必须使用Intent.FLAG_ACTIVITY_NEW_TASK标志的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PendingIntent android.app.PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags)

 

public static PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags)

Since: API Level 1

Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent). Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent.

找到一个PendingIntent用来开始启动一个新的activity,类似于调用Context.startActivity(Intent),注意这个Activity将在当前存在的Activity外部启动,你必须使用Intent中的Intent.FLAG_ACTIVITY_NEW_TASK标志登陆这个Acitivity.

 

Parameters

context  The Context in which this PendingIntent should start the activity.

requestCode  Private request code for the sender (currently not used).

intent  Intent of the activity to be launched.

flags  May be FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT, or any of the flags as supported by Intent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens.

 

Returns

Returns an existing or new PendingIntent matching the given parameters. May return null only if FLAG_NO_CREATE has been supplied.

例如:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,

        new Intent(this, NotifyingController.class), 0);

 

// Set the info for the views that show in the notification panel.

notification.setLatestEventInfo(this, getText(R.string.status_bar_notifications_mood_title),

               text, contentIntent);

package com.example.coursemanager.receivers; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Build; import androidx.core.app.NotificationCompat; import com.example.coursemanager.R; import com.example.coursemanager.activities.MainActivity; public class ReminderReceiver extends BroadcastReceiver { private static final String CHANNEL_ID = "course_reminder"; @Override public void onReceive(Context context, Intent intent) { String courseName = intent.getStringExtra("course_name"); String room = intent.getStringExtra("room"); String time = intent.getStringExtra("time"); createNotificationChannel(context); Intent mainIntent = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity( context, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT); Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_school) .setContentTitle("即将上课: " + courseName) .setContentText(time + " @ " + room) .setPriority(NotificationCompat.PRIORITY_HIGH) .setContentIntent(pendingIntent) .setAutoCancel(true) .build(); NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(0, notification); } private void createNotificationChannel(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( CHANNEL_ID, "Course Reminders", NotificationManager.IMPORTANCE_HIGH); channel.setDescription("Notifications for upcoming courses"); NotificationManager manager = context.getSystemService(NotificationManager.class); manager.createNotificationChannel(channel); } } }该代码的功能是什么,怎么使用,请提供使用示例
06-09
错误: 找不到符号 Intent intent = new Intent(this,notification.class); ^ 符号: 类 notification 位置: 类 MainActivitypackage com.example.myapplication; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.NotificationCompat; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.graphics.BitmapFactory; import android.graphics.Color; import android.os.Build; import android.view.View; public class MainActivity extends AppCompatActivity { private static final String TAG = "notification"; // 通知管理器对象 private NotificationManager manager; // 通知对象 private Notification notification; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notifacation); // 1、创建【NotificationManager】通知管理器对象 manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); // 2、设置通知渠道 // 判断是否Android 8.0版本以上 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 创建【NotificationChannel】通知渠道 NotificationChannel channel = new NotificationChannel("test", "通知的内容: 您有一条新的消息!", NotificationManager.IMPORTANCE_HIGH); // 通知管理器添加通知渠道 manager.createNotificationChannel(channel); } // 设置跳转页面 Intent intent = new Intent(this,notification.class); // 创建PendingIntent PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE); // 3、创建【Notification】通知对象 notification = new NotificationCompat.Builder(this, "test") .setContentTitle("测试通知标题") .setContentText("通知的具体内容: 长路漫漫,唯剑作伴") // 通知显示的小图标 .setSmallIcon(R.drawab
03-21
@RequiresApi(api = Build.VERSION_CODES.O) @Override public void onCreate() { super.onCreate(); Log.d(“MyService”, “onCreate”); Intent intent = new Intent(this,MainActivity.class); PendingIntent pi = PendingIntent.getActivity(this,0,intent, PendingIntent.FLAG_IMMUTABLE); String channelId = "my_service_channel"; String channelName = "My Background Service"; int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel(channelId, channelName, importance); channel.setDescription("Foreground Service Channel"); NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); manager.createNotificationChannel(channel); Notification notification = new NotificationCompat.Builder(this,channelId) .setContentTitle("this is a title") .setContentText("this is a text") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) .setContentIntent(pi) .build(); //manager.notify(1,notification); startForeground(1,notification); }上述代码报错:FATAL EXCEPTION: main Process: com.example.cameratest, PID: 14112 java.lang.RuntimeException: Unable to create service com.example.cameratest.MyService: android.app.MissingForegroundServiceTypeException: Starting FGS without a type callerApp=ProcessRecord{b959cbf 14112:com.example.cameratest/u0a194} targetSDK=36 at android.app.ActivityThread.handleCreateService(ActivityThread.java:4705) at android.app.ActivityThread.-$$Nest$ mhandleCreateService(Unknown Source:0) at android.app.ActivityThread$ H.handleMessage(ActivityThread.java:2305) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:205) at android.os.Looper.loop(Looper.java:294) at android.app.ActivityThread.main(ActivityThread.java:8225) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$ MethodAndArgsCaller.run(RuntimeInit.java:573) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1058) Caused by: android.app.MissingForegroundServiceTypeException: Starting FGS without a type callerApp=ProcessRecord{b959cbf 14112:com.example.cameratest/u0a194} targetSDK=36 at android.app.MissingForegroundServiceTypeException$ 1.createFromParcel(MissingForegroundServiceTypeException.java:53) at android.app.MissingForegroundServiceTypeException$ 1.createFromParcel(MissingForegroundServiceTypeException.java:49) at android.os.Parcel.readParcelableInternal(Parcel.java:4870) at android.os.Parcel.readParcelable(Parcel.java:4852) at android.os.Parcel.createExceptionOrNull(Parcel.java:3052) at android.os.Parcel.createException(Parcel.java:3041) at android.os.Parcel.readException(Parcel.java:3024) at android.os.Parcel.readException(Parcel.java:2966) at android.app.IActivityManager$ Stub$ Proxy.setServiceForeground(IActivityManager.java:6795) at android.app.Service.startForeground(Service.java:775) at com.example.cameratest.MyService.onCreate(MyService.java:79) at android.app.ActivityThread.handleCreateService(ActivityThread.java:4692) at android.app.ActivityThread.-$$Nest$ mhandleCreateService(Unknown Source:0) at android.app.ActivityThread$ H.handleMessage(ActivityThread.java:2305) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:205) at android.os.Looper.loop(Looper.java:294) at android.app.ActivityThread.main(ActivityThread.java:8225) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$ MethodAndArgsCaller.run(RuntimeInit.java:573) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1058)
07-11
行 34445: 08-05 16:56:39.165 12677 12677 W System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.nfc.NfcAdapter.enableForegroundDispatch(android.app.Activity, android.app.PendingIntent, android.content.IntentFilter[], java.lang.String[][])' on a null object reference 行 34446: 08-05 16:56:39.165 12677 12677 W System.err: at com.octopus.f0.F0Detector.enableForegroundDispatch(Unknown Source:58) 行 34447: 08-05 16:56:39.165 12677 12677 W System.err: at com.octopus.f0.F0Detector.onResume(Unknown Source:0) 行 34448: 08-05 16:56:39.165 12677 12677 W System.err: at com.octopuscards.nfc_reader.ui.general.activities.NfcActivity.onResume(Unknown Source:78) 行 34449: 08-05 16:56:39.165 12677 12677 W System.err: at com.octopuscards.nfc_reader.ui.general.activities.LocaleActivity.onResume(Unknown Source:0) 行 34450: 08-05 16:56:39.165 12677 12677 W System.err: at com.octopuscards.nfc_reader.ui.general.activities.GeneralActivity.onResume(Unknown Source:0) 行 34451: 08-05 16:56:39.165 12677 12677 W System.err: at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1740) 行 34452: 08-05 16:56:39.165 12677 12677 W System.err: at android.app.Activity.performResume(Activity.java:9369) 行 34453: 08-05 16:56:39.165 12677 12677 W System.err: at android.app.ActivityThread.performResumeActivity(ActivityThread.java:5742) 行 34454: 08-05 16:56:39.165 12677 12677 W System.err: at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:5803) 行 34455: 08-05 16:56:39.165 12677 12677 W System.err: at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:64) 行 34456: 08-05 16:56:39.165 12677 12677 W System.err: at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:60) 行 34457: 08-05 16:56:39.165 12677 12677 W System.err: at android.app.servertransaction.TransactionExecutor.executeLifecycleItem(TransactionExecutor.java:230) 行 34458: 08-05 16:56:39.165 12677 12677 W System.err: at android.app.servertransaction.TransactionExecutor.executeTransactionItems(TransactionExecutor.java:112) 行 34459: 08-05 16:56:39.165 12677 12677 W System.err: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:86) 行 34460: 08-05 16:56:39.165 12677 12677 W System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2914) 行 34461: 08-05 16:56:39.165 12677 12677 W System.err: at android.os.Handler.dispatchMessage(Handler.java:112) 行 34462: 08-05 16:56:39.165 12677 12677 W System.err: at android.os.Looper.loopOnce(Looper.java:288) 行 34463: 08-05 16:56:39.165 12677 12677 W System.err: at android.os.Looper.loop(Looper.java:393) 行 34464: 08-05 16:56:39.165 12677 12677 W System.err: at android.app.ActivityThread.main(ActivityThread.java:9564) 行 34465: 08-05 16:56:39.165 12677 12677 W System.err: at java.lang.reflect.Method.invoke(Native Method) 行 34466: 08-05 16:56:39.165 12677 12677 W System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:600) 行 34467: 08-05 16:56:39.165 12677 12677 W System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1010)
最新发布
08-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值