【问题标题】:Different android version bug不同的android版本错误
【发布时间】:2013-10-13 03:34:25
【问题描述】:

我有一个带有状态栏图标的简单应用。当用户最小化应用程序时,会出现状态栏图标。当用户单击该图标时,应用程序应恢复并继续工作。我已经在 android 2.3.4 上进行了测试,它可以工作,但是在 4.0 android 版本上,我的应用程序没有恢复并继续工作,而是打开了我的应用程序的新版本。如何制作通用通知码,不用担心安卓版本?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_jajko);

    text = (TextView) findViewById(R.id.tvTime);
    play = (Button) findViewById(R.id.butStart);
    miekko = (Button) findViewById(R.id.butMiekko);
    srednio = (Button) findViewById(R.id.butSrednio);
    twardo = (Button) findViewById(R.id.butTwardo);

    miekko.setOnClickListener(this);
    srednio.setOnClickListener(this);
    twardo.setOnClickListener(this);
    play.setOnClickListener(this);

    mp = MediaPlayer.create(Jajko.this, R.raw.alarm);

    showNotification(this);

}

public static void showNotification(Context context) {
    final Intent result_intent = new Intent(context, Jajko.class);

    result_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    TaskStackBuilder stack_builder = TaskStackBuilder.create(context);
    stack_builder.addParentStack(Jajko.class);
    stack_builder.addNextIntent(result_intent);

    PendingIntent pending_intent = stack_builder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);

    android.support.v4.app.NotificationCompat.Builder builder = new android.support.v4.app.NotificationCompat.Builder(
            context);

    Resources res = context.getResources();
    builder.setContentIntent(pending_intent)
            .setSmallIcon(R.drawable.icon)
            .setLargeIcon(
                    BitmapFactory.decodeResource(res, R.drawable.icon))
            .setTicker("Egg Timer").setWhen(System.currentTimeMillis())
            .setAutoCancel(false).setContentTitle("Egg Timer")
            .setContentInfo("").setContentText("");
    Notification n = builder.build();
    n.flags = Notification.FLAG_ONGOING_EVENT;

    notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIF_ID, n);
}

【问题讨论】:

    标签: android android-layout android-intent android-emulator android-version


    【解决方案1】:

    documentation for TaskStackBuilder 表示它在 3.0 之前和之后的 Android 上的行为不同。我没有将它用于未决意图,所以我不确定会发生什么。

    尝试使用堆栈构建器完全删除这三行,并将待处理的意图行更改为:

    PendingIntent pending_intent = PendingIntent.getActivity(getBaseContext(), 
        0, result_intent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    【讨论】:

    • 嗨,现在当我输入您的代码时,我遇到了一个错误:无法从 PendingIntent 行中的 ContextWrapper 类型中对非静态方法 getBaseContext() 进行静态引用 pending_intent = PendingIntent.getActivity( getBaseContext(), 0, result_intent, PendingIntent.FLAG_UPDATE_CURRENT);
    • 改用context。我没有注意到您使用的是静态方法。
    猜你喜欢
    • 1970-01-01
    • 2010-09-14
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    相关资源
    最近更新 更多