我想在用户单击通知时打开活动。我知道这个问题是重复的,但找不到解决方案,这就是我所做的
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, ResultActivity.class);
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent =
    PendingIntent.getActivity(
    this,
    0,
    resultIntent,
    PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = 
    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build()); …我的应用程序接收推送并根据推送类型打开不同的活动。
我使用TaskStackBuilder待处理的 Intent 来在我的清单中结合创建一个合成后堆栈android:parentActivityName。
到目前为止,一切都很简单。当应用程序未启动时,一切按预期工作。但是,如果应用程序位于后台(任务正在运行),待处理的 Intent 还会使用清单中定义的父级启动我所需的活动,但会重置现有任务。问题是用户同时启动的其他活动也会被清除。   
所以想要实现的是:
我似乎无法让它与TaskStackBuilder.
我很乐意获得一些见解。
android android-intent android-notifications android-pendingintent taskstackbuilder
我有一个活动,它通过通知启动前台服务。如果相关,服务也可以设置为在启动时启动,即。我希望它也能够在没有 Activity 的情况下启动。
我希望它是这样,如果我点击通知,然后我去我的活动,我可以停止服务,更改设置等。
我将带有 Activity 类的 Intent 提供给 PendingIntent 以获取前台服务的通知。
如果我点击通知,它会在我的应用程序的系统设置中打开应用程序信息(您可以在其中强制停止、卸载等),而不是活动本身。我怎样才能让它做后者?
这是在服务启动时创建通知的代码:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Context con = getApplicationContext();
    String chanID = "com.blah.MyApp";
    NotificationChannel chan = new NotificationChannel(chanID, getString(R.string.app_name), NotificationManager.IMPORTANCE_NONE);
    NotificationManager manager = (NotificationManager) con.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.createNotificationChannel(chan);
    PendingIntent penitent = PendingIntent.getActivity(con, 1, new Intent(getBaseContext(), MyApp.class), 0);
    Notification notif = new NotificationCompat.Builder(con, chanID).setContentIntent(penitent).build();
    startForeground(1, notif);
...
我也试过用
 Intent.makeMainActivity(new ComponentName("com.blah", "MyApp"));
结果相同。我想知道问题是否与上下文有关?
编辑:我也无法更改通知上的文字。
日志猫:
2019-04-12 09:50:56.062 1395-6289/? I/ActivityManager: START u0 {act=android.settings.APPLICATION_DETAILS_SETTINGS dat=package:com.blah cmp=com.android.settings/.applications.InstalledAppDetails} from uid …android android-intent android-pendingintent android-8.1-oreo
例外:面向 S+(版本 31 及更高版本)要求在创建 PendingIntent 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一
导致异常的代码:WorkManager.getInstance(context).createCancelPendingIntent(id)
build.gradle 选项:
编译SdkVersion 31
buildToolsVersion“30.0.3”(版本 31.0.0 出现错误:安装的构建工具修订版 31.0.0 已损坏。使用 SDK 管理器删除并重新安装)。
模拟器:API 31(使用 API S 一切正常)
依赖项:
// 工作管理器
实现“androidx.work:work-runtime-ktx:2.7.0-alpha05”
实施 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava'
我认为问题可能出在BuildCompat.java的这个方法中:
/**
 * Checks if the device is running on a pre-release version of Android S or a release version of
 * Android S or newer.
 * <p>
 * <strong>Note:</strong> When Android S is finalized for release, this method will …给定两个具有不同数据A和B的相同小部件,当小部件A的数据显示在小部件启动的活动中时,按下后退按钮然后按下小部件B它一切正常.然而,当显示小部件A的数据时,按下主页按钮然后按下小部件B,仍然显示小部件A的数据.从Android  - 当从具有不同附加功能的小部件启动相同的活动时,如何防止从HOME按钮返回后显示相同的实例?它表明我的问题是PendingIntents(对于小部件A和B)两者都是相同的,并且仅在附加内容上有所不同,因此被缓存.即附加功能是widget ID,它是我需要检索widget-press动作的所有数据的关键.一个建议是使用不同的数据,但我似乎无法让这个工作.这是代码:
Intent intent = new Intent(context, WidgetActivity.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.withAppendedPath(Uri.parse("droidln://widget/id/"),
               String.valueOf(appWidgetId)));
PendingIntent pendingIntent = 
               PendingIntent.getActivity(context, appWidgetId, intent, 0);
添加setData到意图产生没有区别.关于如何解决缓存挂起意图问题的任何想法?我也尝试过:
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
和
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
和
intent.setAction("actionstring" + System.currentTimeMillis());
一切都没有效果.
我正在为我的应用程序开发一个小部件,我想将小部件中的特定数据传递给应用程序.用户可以添加多个版本的小部件,我需要检测它来自哪个小部件,以便我可以在应用程序中显示特定信息.
到目前为止我的工作,但只有当我完全退出我的应用程序(反复按下后退按钮),否则它似乎没有检测到传入的数据.
这个代码我必须将数据从小部件传递到应用程序:
public class WidgetProvider extends AppWidgetProvider  {
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;
        for (int i=0; i<N; i++) 
            UpdateWidget(context, appWidgetManager, appWidgetIds[i]);
    }
    public static void UpdateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId)
    {
        Intent intent = new Intent(context, Main.class);
        final Bundle bun = new Bundle();
        bun.putInt("widgetId", appWidgetId);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtras(bun);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
        views.setOnClickPendingIntent(R.id.widgetText, pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
} …当我们将0作为标志传递给PendingIntent时,如下所示:
PendingIntent pi=PendingIntent.getActivity(this, 1, i, 0);
它是否遵循任何标志规则意味着默认情况下0对应于任何标志.
如果我们创建另一个PendingIntent为
 PendingIntent pii=PendingIntent.getActivity(this, 1, i, 0);
它是否与之前相同,如果我对Intent中的数据进行任何更改,它现在是否与Intent中的新数据相对应,或者仍然具有旧数据.
我面临的另一个问题是我试图检查旗帜
PendingIntent.FLAG_NO_CREATE
我写了以下代码:
Intent i=new Intent(this,NotifResult.class);
        i.putExtra("number",50);
        PendingIntent pi=PendingIntent.getActivity(this, 1, i, 0);
NotificationCompat.Builder nb=new NotificationCompat.Builder(this);
        nb.setSmallIcon(R.drawable.ic_launcher);
        nb.setTicker("ticker is here");
        nb.setWhen(System.currentTimeMillis())
        .setContentTitle("just the title")
        .setContentText("and the description")
        .setAutoCancel(true)
        .setDefaults(Notification.DEFAULT_ALL)
        .setContentIntent(pi);
Notification notif=nb.build();
        NotificationManager nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(11, notif);
        i.putExtra("number",80);
        PendingIntent pisecond=PendingIntent.getActivity(this, 1, i, PendingIntent.FLAG_NO_CREATE);
        if(pi.equals(pisecond))
            Log.v("check","they are equal");
        else
            Log.v("check", "they are not equal");
        notif.contentIntent=pisecond;
        nm.notify(11, notif);
根据文档,如果存在existign对象,PendingIntent.FLAG_NO_CREATE不会创建任何新对象.我在NotifResult活动中打印数字的值,其中数字值将是80而不是50,因为它应该使用具有旧值的现有意图(根据我的理解).明确更新为什么输出将来80.日志显示对象与预期的一样.
谢谢
AlarmManager的文档似乎暗示(但并不直接明确要求)您传入任何方法的PendingIntentset()应该是BroadcastReceiver类型,但我测试了传入其他组件类型(如IntentService),它似乎工作得很好.
将非BroadcastReceiver Intents与AlarmManager一起使用是否安全?
android broadcastreceiver alarmmanager intentservice android-pendingintent
Android操作系统在内存不足时会终止进程.场景:Android杀死应用程序进程,我通过Android启动器或最近任务列表(长按主页按钮)重新打开它.我可以使用以下方法检查Android是否在最近查看的活动的onCreate()方法中杀死了我的应用程序进程:
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        // Re-initialise things that killing the app would have destroyed
    }
}
但是,如果Android杀死应用程序进程并使用打包在PendingIntent中的Intent通过Notification重新打开它,我不知道如何确定应用程序进程是否被Android杀死.随后我不会重新初始化杀死应用程序进程会破坏的内容.
有没有办法确定Android是否在从通知中打开新活动时杀死了应用程序进程?
我找到了一个解决这个问题的hacky解决方案.使用:Android:在点击通知时始终启动顶级活动我可以在堆栈顶部打开活动,如果Android杀死应用程序进程并处理重新初始化,则会传递savedInstanceState.然后,每个活动负责使用原始通知意图中的额外内容将用户重定向到相应的活动.此方案的意图设置如下:
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
我是否可以在Intent上设置一个Action,Category或Flag,它将模拟重新打开应用程序进程,就好像是由用户完成但是在新的Intent/Activity上?
编辑:澄清最后一个问题(虽然看起来我的婴儿对Android的理解让我失望所以它可能没有意义):我是否可以在Intent上设置Action,Category或Flag,就像上面的代码段一样,这将允许我确定应用程序进程是否已被操作系统杀死?
android android-intent android-notifications android-pendingintent android-os-handler
我已经在整个互联网上进行了搜索,但没有找到太多帮助。我正在制作一个警报应用程序,其中我正在与警报管理器一起使用未决意图。我想基于sqlite数据库表的特定ID设置两个警报。对于第一个警报,我将待处理的意图请求代码设置为肯定ID,对于第二个意图,我将待处理的意图请求代码设置为否定ID(-1 * id)。这是代码
PendingIntent pi = PendingIntent.getBroadcast(context, _id, i, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pi = PendingIntent.getBroadcast(context, -1 *_id, i, PendingIntent.FLAG_UPDATE_CURRENT);
_id可以为负吗?
android broadcastreceiver android-pendingintent android-studio