无法解析方法setLatestEventInfo

时间:2015-09-02 06:10:32

标签: android

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

  

无法解析方法setLatestEventinfo

这是我的代码段:

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);
}

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

4 个答案:

答案 0 :(得分:80)

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

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 {

}

答案 1 :(得分:39)

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

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

答案 2 :(得分:29)

您写信给 以使用setLatestEventInfo。这是否意味着您已准备好让您的应用与最新的Android版本兼容?我强烈建议您使用包含NotificationCompat类的support library v4来使用API​​ 4及更高版本的app。

如果你真的不想使用支持库(即使使用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();
}

答案 3 :(得分:-3)

转到项目 - &gt;属性并设置android-target 21