我使用一个AlarmManager来触发一个广播信号的意图。以下是我的代码:

AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, Wakeup.class);
try
{
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
    Long elapsed +=  // sleep time;
    mgr.set(AlarmManager.RTC_WAKEUP, elapsed, pi);
}
catch(Exception r)
{
    Log.v(TAG, "RunTimeException: " + r);
}

我从一个活动调用这个代码,所以我不知道我如何得到以下错误…

ERROR/AndroidRuntime(7557): java.lang.RuntimeException: Unable to start receiver com.wcc.Wakeup: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

当前回答

在我的情况下,我已经使用context的startActivity,改变后与ActivityName。这一点。它解决了。我使用的是来自util类的method。

希望它能帮助到一些人。

其他回答

当你想在你的应用程序中打开一个活动时,你可以调用startActivity()方法,并将Intent作为参数。意图就是你想要打开的活动。 首先,您必须创建一个该意图的对象,其中第一个参数是上下文,第二个参数是目标活动类。

Intent intent = new Intent(this, Activity_a.class);
startActivity(intent);

希望这能有所帮助。

你没有粘贴调用startActivity的部分,这是有趣的部分。

你可以在服务上下文中调用startActivity,也可以在应用上下文中调用。

在startActivity调用之前打印“this”到log cat,看看它指的是什么,有时会意外地使用内部的“this”。

对于同一个活动的多个实例,使用下面的代码段,

注意:这个片段,我在我的活动之外使用。确保你的AndroidManifest文件不包含android:launchMode="singleTop|singleInstance"。如果需要,你可以把它改成android:launchMode="standard"。

Intent i = new Intent().setClass(mActivity.getApplication(), TestUserProfileScreenActivity.class);  
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

// Launch the new activity and add the additional flags to the intent
mActivity.getApplication().startActivity(i);

这对我来说很好。霍普,这为别人节省了时间。如果有人找到更好的方法,请与我们分享。

这是代码。我已经在一个壁纸api实现应用程序中实现了它。在我的应用程序中,特别是当用户单击壁纸图像时,它将恢复到全屏视图。

holder.imageView.setOnClickListener {
          val i = Intent(context,FullScreenWallpaper::class.java)
          i.putExtra("raheel", wallpaperModelList2[position])
          i.flags = Intent.FLAG_ACTIVITY_NEW_TASK
          context.startActivity(i)
      }

如果你的android版本低于android - 6,那么你需要添加这一行,否则它将在android - 6之上工作。

...
Intent i = new Intent(this, Wakeup.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
...