IT TIP

setLatestEventInfo 메서드를 확인할 수 없습니다.

itqueen 2020. 12. 6. 22:27
반응형

setLatestEventInfo 메서드를 확인할 수 없습니다.


알림 작업 중이며을 사용해야 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);
}

또는 그들이 그렇게하는 다른 방법이라면 친절하게 저에게 제안하십시오.


아래는 알림을 사용하는 간단한 예입니다. 도움이되기를 바랍니다.

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 {

}

에 따르면 : https://developer.android.com/sdk/api_diff/23/changes/android.app.Notification.html

이 메서드는 M (api 23)에서 제거되었습니다. 따라서 컴파일 SDK 버전이 api 23+로 설정된 경우이 문제가 표시됩니다.


You write you have to use setLatestEventInfo. Does it mean you are ready to have your app not compatible with more recent Android versions? I strongly suggest you to use the support library v4 that contains the NotificationCompat class for app using API 4 and over.

If you really do not want to use the support library (even with Proguard optimization, using NotificationCompat will add a good 100Ko on the final app), an other way is to use reflection. If you deploy your app on an Android version that still has the deprecated setLatestEventInfo, first of all you should check if you are in such an environment, and then you use reflection to access the method.

This way, Android Studio or the compiler will not complain, since the method is accessed at runtime, and not at compile time. For instance :

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

Go to project -> properties and set android-target 21

참고URL : https://stackoverflow.com/questions/32345768/cannot-resolve-method-setlatesteventinfo

반응형