相关疑难解决方法(0)

通知传递旧的Intent Extras

我通过以下代码在BroadcastReceiver中创建通知:

String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
        int icon = R.drawable.ic_stat_notification;
        CharSequence tickerText = "New Notification";
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        long[] vibrate = {0,100,200,200,200,200};
        notification.vibrate = vibrate;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        CharSequence contentTitle = "Title";
        CharSequence contentText = "Text";
        Intent notificationIntent = new Intent(context, NotificationActivity.class);
        notificationIntent.putExtra(Global.INTENT_EXTRA_FOO_ID, foo_id);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        int mynotification_id = 1;

        mNotificationManager.notify(mynotification_id, notification);
Run Code Online (Sandbox Code Playgroud)

当我点击通知时,它会打开NotificationActivity,在Activity中我可以从Intent-Bundle中检索foo_id(例如1)

但是,如果触发另一个通知并再次单击它,则活动仍会从Intent-Bundle接收"旧"值(1).我试图用clear()清除包,但收到的效果相同.我觉得我的代码错了......

android android-notifications android-notification-bar android-pendingintent

129
推荐指数
4
解决办法
3万
查看次数

PendingIntent不会发送Intent附加内容

MainActicity 开始RefreshServiceIntent具有boolean所谓的多余isNextWeek.

RefreshService做了一个在用户点击它时Notification启动我MainActivity的.

这看起来像这样:

    Log.d("Refresh", "RefreshService got: isNextWeek: " + String.valueOf(isNextWeek));

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.putExtra(MainActivity.IS_NEXT_WEEK, isNextWeek);

    Log.d("Refresh", "RefreshService put in Intent: isNextWeek: " + String.valueOf(notificationIntent.getBooleanExtra(MainActivity.IS_NEXT_WEEK,false)));
    pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    builder = new NotificationCompat.Builder(this).setContentTitle("Title").setContentText("ContentText").setSmallIcon(R.drawable.ic_notification).setContentIntent(pendingIntent);
    notification = builder.build();
    // Hide the notification after its selected
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(NOTIFICATION_REFRESH, notification);
Run Code Online (Sandbox Code Playgroud)

你可以看到notificationIntent应该有boolean额外IS_NEXT_WEEK的值,isNextWeek其中放入了PendingIntent.

当我现在点击这个时, …

android android-intent android-service android-pendingintent

117
推荐指数
2
解决办法
4万
查看次数

来自通知的意图没有额外内容

这似乎是一个常见的问题,我经历了我已经找到的所有相关问题: 活动没有获得新意图,为什么在android通知意图中没有发送额外数据(整数)?,Notification传递旧的Intent Extras,不能为通知中的意图,android挂起意图通知问题添加额外内容 ; 但仍然无法弄清楚这一点.

问题是一样的.我设置了一个带有PendingIntent的通知,其中包含一些额外的信息,而我却没有在另一方面得到它.

以下是生成通知的代码:

Notification notification = new Notification(R.drawable.icon, getResources().getString(R.string.notification_ticker), System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;

Intent start_test = new Intent(this, MyActivity.class);
start_test.putExtra("start_test", true);
start_test.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent pi = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), start_test, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(this, getResources().getString(R.string.notification_title), getResources().getString(R.string.notification_content, expired), pi);
nm.notify(NOTIFICATION_ID, notification);
Run Code Online (Sandbox Code Playgroud)

另一方面:

boolean start_test=getIntent().getBooleanExtra("start_test", false);
Run Code Online (Sandbox Code Playgroud)

捆绑实际上不存在(getExtras()返回null).

我尝试了不同的标志为PendingIntent.getActivity(FLAG_UPDATE_CURRENT,FLAG_CANCEL_CURRENT,FLAG_ONE_SHOT),没有这些帮助的.

如代码所示,我使用getCurrentMillis来确保requestID发生变化....

还发现在MyActivity中,onCreateonNewIntent没有被调用.只是onResume.即使FLAG_ACTIVITY_NEW_TASK设定......?

我错过了一些非常简单的事吗?

android android-intent android-notifications

43
推荐指数
3
解决办法
3万
查看次数

IntentService创建的通知始终使用错误的Intent

问题

当用户按下Send "Button 1"(向下滚动以查看应用程序的构造)时,将Notification创建一个新的RefreshService.如果用户按下此通知,则MainActivity实例启动并接收String具有Button 1超过该值的值Intent.

显示该值.

当用户现在按下时,Send "Button 2"Notification创建一个新的RefreshService.如果用户按下此通知,则会MainActivity启动一个实例,并接收一个ALSO值超过该值的ALSO.String Button 1Intent

所以你可以猜测,通常应该有价值Button 2.

当用户按下的第一个按钮时,Send "Button 2"总会Button 2发送.

获得第二个按钮值的唯一解决方案是重新启动手机并先按下第二个按钮.即使强行关闭也行不通.

我知道我也可以用另一种方式更改UI.但是我需要在应用程序中使用这种方法,我需要用另一个重新启动'MainActivity',Intent因此方法应该是相同的.

施工

主要活动

public class MainActivity extends Activity implements View.OnClickListener {
    public static final String RECEIVED = "received"; …
Run Code Online (Sandbox Code Playgroud)

android android-intent android-notifications intentservice android-activity

27
推荐指数
1
解决办法
1万
查看次数

如何设置点击监听器以进行通知?

通过AlarmManager启动服务时,我使用以下代码启动通知:

nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "App";
CharSequence message = "Getting Latest Info...";
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
Notification notif = new Notification(R.drawable.icon,
    "Getting Latest Info...", System.currentTimeMillis());
notif.setLatestEventInfo(this, from, message, contentIntent);
nm.notify(1, notif);
Run Code Online (Sandbox Code Playgroud)

如何设置此项目的意图,以便当用户点击它时,它会启动某个活动?

android notificationmanager

23
推荐指数
3
解决办法
8万
查看次数

将数据从服务发送到活动

我通过通知将数据从服务发送到活动时遇到问题,我点击通知活动被调用但是当我尝试通过包添加一些参数时我无法获得该被调用的意图中的参数,我已经通过了link 如何从通知点击发送参数到活动?

但仍然没有运气.

其他人有同样的问题吗?

提前致谢.

java android

13
推荐指数
1
解决办法
3万
查看次数

没有调用Android OnNewIntent

我看到了几种方法,我尝试了一切,但无法使它工作.我不知道为什么它如此复杂,在文档中它看起来如此简单!我想通过通知触发OnNewIntent(用户在通知栏中单击它).

目前我已将Activity设置为singleTop

<activity
    android:name="-.-.-.MainMenuActivity"
    android:launchMode="singleTop"
    android:screenOrientation="portrait" >
</activity>
Run Code Online (Sandbox Code Playgroud)

这是我创建通知的代码:

public void showNotification(String text, String componentId){
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.logo)
            .setContentTitle("Title")   
            .setAutoCancel(true)
            .setContentText(text);

    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, MainMenuActivity.class);
    if(!componentId.equalsIgnoreCase("")){                         
        resultIntent.putExtra("componentId", componentId);
    }   
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);
    mBuilder.setFullScreenIntent(resultPendingIntent, false);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(0, mBuilder.build());
}
Run Code Online (Sandbox Code Playgroud)

这是我的MainMenuActivity中的OnNewIntent方法:

@Override
protected void …
Run Code Online (Sandbox Code Playgroud)

notifications android push-notification onnewintent

12
推荐指数
2
解决办法
9613
查看次数

无法在通知中添加额外内容

我正在从具有额外整数意图的服务创建通知.出于某种原因,这并不像看起来那么容易.这就是我正在做的事情:

Notification notification = new Notification(R.drawable.notification_icon, "Title", 0);
Intent relaunchIntent = new Intent(this, SomeClass.class);
relaunchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
relaunchIntent.putExtra("load", getId());
notification.setLatestEventInfo(this, "Title", "Tap here to open", PendingIntent.getActivity(this, 0, relaunchIntent, 0);
notificationManager.notify(1234, notification);
Run Code Online (Sandbox Code Playgroud)

然后从SomeClass,它读起来像这样......

if(getIntent().getExtras() != null)
    load(getIntent().getExtras().getInt("load", -1));
Run Code Online (Sandbox Code Playgroud)

奇怪的是,当我从getInt()方法中读取值时,我得到了不一致的结果.有时我会得到我在之前的通知中设置的值的值.例如,当我将额外设置为4时,我得到值为3.我知道这非常令人困惑和奇怪,这就是为什么我想知道是否有人经历过这样的事情以及你可能做了些什么来修复它.谢谢!

service notifications android android-intent extra

3
推荐指数
1
解决办法
2474
查看次数

将小部件ID传递给活动

在我的小部件提供程序中,我有以下代码:

for (int widget_id : appWidgetIds) {
  Log.d(TAG, "Widget Id: " + widget_id);
  RemoteViews remoteViews;

  Widget widget;

  while (true) {
    try {
      widget = Widget.load(context, Widget.class, widget_id);
      break;
    }
    catch (Exception ex) {
      Log.e(TAG, "Exception!", ex);
    }
  }

  // Widget is assigned to user
  if (widget != null) {
    remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_with_user);
    remoteViews.setCharSequence(R.id.user_text, "setText", widget.user.name);
    remoteViews.setUri(R.id.avatar, "setImageURI", Uri.parse(widget.user.avatar));
  }
  else {
    remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
    Intent configIntent = new Intent(context, ConfigureWidget.class);
    configIntent.setAction(ACTION_WIDGET_CONFIGURE);
    configIntent.putExtra("widget_id", widget_id);

    PendingIntent configPendingIntent = …
Run Code Online (Sandbox Code Playgroud)

android android-widget

2
推荐指数
1
解决办法
2416
查看次数

显示对话框时出现BadTokenException

我在Android应用程序上有一个活动,它启动与我的服务器的同步过程.此过程消耗大量内存和处理,并且需要一些时间才能完成.

该过程完成后,将向用户显示AlertDialog,显示该过程是否已成功完成.

如果我离开活动前景,一切都会按预期工作.但是,有时,当我离开应用程序时,后台进程继续其工作,但是,当我返回到应用程序时,它崩溃并出现以下错误:

android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌android.os.BinderProxy@4086ea48无效; 你的活动在运行吗?

这个错误可能是因为操作系统破坏了活动吗?如果是,那么进程(在另一个线程上运行)如何继续运行?而且,如果我们确认问题是由活动的破坏引起的,我怎么能处理这种情况,避免崩溃并向用户显示对话框?

这是一些代码......

public class ActSincronizacao extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.sincronizacao);

        //Prepare so starts the process
        [...]

        // Defines a Handler that will be called when the process gets finished
        oSocketEngine.addProgressUpdateFinishListener(new ProgressUpdateFinishListener() {

            @Override
            public void ProgressUpdateFinishOccurred(final ProgressUpdateFinish evt) {

                //Do some processing that doesn´t envolve UI
                [...]

                // Process that envolves UI
                runOnUiThread(new Runnable() {

                    public void run() {

                        if ((boolean)evt.getArgs().getResult()) {

                            //If the process …
Run Code Online (Sandbox Code Playgroud)

android dialog android-alertdialog android-activity

1
推荐指数
1
解决办法
4534
查看次数