【问题标题】:Open mutiple intents separately in android在android中分别打开多个intent
【发布时间】:2014-08-02 08:00:06
【问题描述】:

我的应用程序生成多个单独的通知,例如,Notification A, Notification B, Notification C. 等...

如果我点击Notification A,A 的详细信息将可见,之后如果我打开Notification C or B, the activity still shows Notification A contents only。如何使其更新以显示相应的通知数据。我正在为每个通知分配唯一的密钥。

这是我生成通知的代码

CharSequence title = title1;
    CharSequence description = notes;

    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Intent intent = new Intent(this.getApplicationContext(), Google_task_notification_preview.class);
    intent.putExtra("CODE", key);        

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
            this.getApplicationContext(), key, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    try
    {

        Drawable d = getResources().getDrawable(ic_launcher);
        Bitmap bitmap = ((BitmapDrawable)d).getBitmap();    

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            getApplicationContext()).setLargeIcon(bitmap)
            .setContentTitle(title)
            .setContentText(description)
            .setContentIntent(pendingNotificationIntent)
            .setSound(soundUri);

    if(priority == 0)
    mBuilder.setSmallIcon(R.drawable.ic_low);
    else
    mBuilder.setSmallIcon(R.drawable.ic_high);  

    mBuilder.setWhen(timestamp);


    Notification notification = mBuilder.build();

    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notification.defaults |= Notification.DEFAULT_SOUND;

        // cancel notification after click
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
        // show scrolling text on status bar when notification arrives
    notification.tickerText = title + "\n" + description;

    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(key, notification);

    }
    catch(SecurityException se)
    {
        se.printStackTrace();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

【问题讨论】:

  • 嗨,在第二个活动中,我正在像int defvalue = 0; mPrimaryKey = getIntent().getIntExtra("CODE", defvalue); Log.i("mPrimaryKey", String.valueOf(mPrimaryKey)); 那样做,以获取密钥。但在这里我总是老钥匙
  • 嗨,如果我在通知活动中保留断点,第一次只有它命中,其余时间它不会:(。
  • 生成 3 到 4 个通知,杀死当前正在运行的实例,然后你尝试一个接一个地打开,在这种情况下它不会工作:(
  • launchMode 的意思是,我没听懂你...你是否通过按主页按钮终止正在运行的活动,然后单独打开通知,你会看到问题
  • 哦,很好!对不起,我没有看到你最后的评论。很高兴你成功了!

标签: android android-intent notifications android-notifications android-alarms


【解决方案1】:

一定会很完美!

清单:

        <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.example.nofiticationexample"
            android:versionCode="1"
            android:versionName="1.0" >

            <uses-sdk
                android:minSdkVersion="16"
                android:targetSdkVersion="18" />

            <application
                android:allowBackup="true"
                android:icon="@drawable/ic_launcher"
                android:label="@string/app_name"
                android:theme="@style/AppTheme" >
                <activity
                    android:name="com.example.nofiticationexample.MainActivity"
                    android:label="@string/app_name" >
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN" />

                        <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
                </activity>
                <activity
                    android:name="com.example.nofiticationexample.NotificationA"
                    android:label="@string/app_name" >
                </activity>
                 <activity
                    android:name="com.example.nofiticationexample.NotificationB"
                    android:label="@string/app_name" >
                </activity>
                 <activity
                    android:name="com.example.nofiticationexample.NotificationC"
                    android:label="@string/app_name" >
                </activity>
            </application>

        </manifest>

主活动:

        package com.example.nofiticationexample;

        import java.util.Calendar;
        import java.util.Date;

        import android.app.Activity;
        import android.app.NotificationManager;
        import android.app.PendingIntent;
        import android.app.TaskStackBuilder;
        import android.content.Context;
        import android.content.Intent;
        import android.net.Uri;
        import android.os.Bundle;
        import android.support.v4.app.NotificationCompat;
        import android.util.Log;
        import android.view.Menu;

        public class MainActivity extends Activity {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                try {
                    /***
                     * generateNotification(Dynamically passing the Class Name)
                     */
                    generateNotification("com.example.nofiticationexample.NotificationB");
                    generateNotification("com.example.nofiticationexample.NotificationC");
                    generateNotification("com.example.nofiticationexample.NotificationA");

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
            }

            public void generateNotification(String className) {
                try {
                    Log.d("className", className);
                    long notificationID = Calendar.getInstance().getTimeInMillis();
                    Class cls = Class.forName(className);
                    Intent resultIntent = new Intent(getApplicationContext(), cls);
                    resultIntent.setData(Uri.parse("content://" + notificationID));
                    PendingIntent pendingIntent = PendingIntent.getActivity(
                            getApplicationContext(), 0, resultIntent,
                            Intent.FLAG_ACTIVITY_NEW_TASK);
                    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
                    mBuilder.setContentTitle("Your Message");
                    mBuilder.setContentText("You've received new message.");
                    mBuilder.setTicker("Your Title !");
                    mBuilder.setSmallIcon(R.drawable.ic_notification);
                    mBuilder.setContentIntent(pendingIntent);
                    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    mNotificationManager.notify((int) notificationID, mBuilder.build());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }

NotificationA 类:

    package com.example.nofiticationexample;

    import android.app.Activity;
    import android.os.Bundle;

    public class NotificationA  extends Activity{

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.notify_1);
        }
    }

【讨论】:

  • +1 感谢您制作示例示例。谢谢你的回答,我想在一个活动中看到多个通知
【解决方案2】:

特别感谢 Mike M,我通过覆盖以下函数解决了这个问题,

 @Override
     protected void onNewIntent(Intent intent) {

         super.onNewIntent(intent);
         // TODO Auto-generated method stub

         mPrimaryKey = intent.getIntExtra("CODE", defvalue);


     }

并在我的通知意图中添加了这些 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP |Intent.FLAG_ACTIVITY_CLEAR_TOP); 标志。

并且在待处理意图中仅添加了 PendingIntent.FLAG_UPDATE_CURRENT 标志

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 2017-01-08
    • 2015-06-11
    • 2012-08-05
    • 2012-09-17
    相关资源
    最近更新 更多