【问题标题】:Cannot Resolve Method setLatestEventInfo无法解决方法 setLatestEventInfo
【发布时间】:2023-03-23 00:51:02
【问题描述】:

我正在处理通知,我必须使用setLatestEventInfo。但是,Android Studio 显示以下错误消息:

无法解析方法 setLatestEventinfo

这是我的代码 sn-p:

private void createNotification(Context context, String registrationID) {
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.icon,"Registration Successfull",System.currentTimeMillis());
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    Intent intent = new Intent(context,RegistrationResultActivity.class);
    intent.putExtra("registration_ID",registrationID);
    PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
    notification.setLatestEventInfo(context,"Registration","Successfully Registered",pendingIntent);
}

或者如果他们是另一种方式,请建议我这样做。

【问题讨论】:

  • setLatestEventInfodeprecated 使用 NotificationCompat.Builder 进行操作

标签: android


【解决方案1】:

下面是一个使用通知的简单示例,请仔细阅读,希望对您有所帮助!

MainActivity.java

public class MainActivity extends ActionBarActivity {

    Button btnShow, btnClear;
    NotificationManager manager;
    Notification myNotication;

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

        initialise();

        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        btnShow.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //API level 11
                Intent intent = new Intent("com.rj.notitfications.SECACTIVITY");

                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 0);

                Notification.Builder builder = new Notification.Builder(MainActivity.this);

                builder.setAutoCancel(false);
                builder.setTicker("this is ticker text");
                builder.setContentTitle("WhatsApp Notification");               
                builder.setContentText("You have a new message");
                builder.setSmallIcon(R.drawable.ic_launcher);
                builder.setContentIntent(pendingIntent);
                builder.setOngoing(true);
                builder.setSubText("This is subtext...");   //API level 16
                builder.setNumber(100);
                builder.build();

                myNotication = builder.getNotification();
                manager.notify(11, myNotication);

                /*
                //API level 8
                Notification myNotification8 = new Notification(R.drawable.ic_launcher, "this is ticker text 8", System.currentTimeMillis());

                Intent intent2 = new Intent(MainActivity.this, SecActivity.class);
                PendingIntent pendingIntent2 = PendingIntent.getActivity(getApplicationContext(), 2, intent2, 0);
                myNotification8.setLatestEventInfo(getApplicationContext(), "API level 8", "this is api 8 msg", pendingIntent2);
                manager.notify(11, myNotification8);
                */

            }
        });

        btnClear.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                manager.cancel(11);
            }
        });
    }

    private void initialise() {
        btnShow = (Button) findViewById(R.id.btnShowNotification);
        btnClear = (Button) findViewById(R.id.btnClearNotification);        
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

    <Button
        android:id="@+id/btnShowNotification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Notification" />

    <Button
        android:id="@+id/btnClearNotification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear Notification" />

</LinearLayout>

以及点击通知会打开的活动,

public class SecActivity extends Activity {

}

【讨论】:

  • 你能看看我的问题吗? stackoverflow.com/questions/35570767/…
  • PendingIntent.getActivity(MainActivity.this, 1, intent, 0);在此方法中,第四个参数用于传递标志。如果你通过 0 意味着什么?
  • @r j,谢谢!这对于基本的通知目的非常有帮助。
  • 11 代表什么?
  • 11 是该通知的ID,如果您想对该通知执行进一步的操作,您可以使用该ID。更多信息,developer.android.com/reference/android/app/…, int, android.app.Notification)
【解决方案2】:

根据:https://developer.android.com/sdk/api_diff/23/changes/android.app.Notification.html

此方法已在 M (api 23) 中删除。因此,如果您的编译 SDK 版本设置为 api 23+,您将看到此问题。

【讨论】:

    【解决方案3】:

    你写你使用setLatestEventInfo。这是否意味着您已经准备好让您的应用程序与更新的 Android 版本不兼容?我强烈建议您将包含 NotificationCompat 类的 support library v4 用于使用 API 4 及更高版本的应用程序。

    如果你真的不想使用支持库(即使有 Proguard 优化,使用 NotificationCompat 会在最终应用上添加一个很好的 100Ko),另一种方法是使用反射。如果您将应用部署在仍然有已弃用的setLatestEventInfo 的 Android 版本上,首先您应该检查您是否处于这样的环境中,然后使用反射来访问该方法。

    这样,Android Studio 或编译器不会报错,因为该方法是在运行时访问的,而不是在编译时访问的。例如:

    Notification notification = null;
    
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        notification = new Notification();
        notification.icon = R.mipmap.ic_launcher;
        try {
            Method deprecatedMethod = notification.getClass().getMethod("setLatestEventInfo", Context.class, CharSequence.class, CharSequence.class, PendingIntent.class);
            deprecatedMethod.invoke(notification, context, contentTitle, null, pendingIntent);
        } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException e) {
            Log.w(TAG, "Method not found", e);
        }
    } else {
        // Use new API
        Notification.Builder builder = new Notification.Builder(context)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(contentTitle);
        notification = builder.build();
    }
    

    【讨论】:

    • 您从字面上理解了“必须”,对彻底性表示敬意。 IMO OP 只是阅读了说明使用该方法的文档。例如Running a service in the foreground 仍然有对该方法的引用。
    • 终于可以用扩展文件将我的应用更新到 API 23+ 非常感谢!!
    • 对于那些在google play扩展库的上下文中遇到这个问题的人,在com.google.android.vending.expansion.downloader包中,github上有一个更新的版本比通过 android studio sdk manager 安装的那个。 github.com/google/play-apk-expansion。此版本不使用已弃用的 setLatestEventInfo 方法。
    【解决方案4】:

    转到项目 -> 属性并设置 android-target 21

    【讨论】:

    • 也许这是正确的(我不知道),但你没有给出任何暗示为什么这会是一个解决方案。
    • 它起作用的原因是因为该 API 在低于 23 的 Android 上可用,并且从 API 级别 23 开始已被弃用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 2021-12-21
    • 2017-09-30
    相关资源
    最近更新 更多