本文介绍了无法解析法setLatestEventInfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的通知,我不得不使用setLatestEventInfo。但是Android工作室说无法解析的方法setLatestEventinfo 。这是我的code段。

I am working on Notifications and I have to use setLatestEventInfo. But Android Studio Says cannot resolve method setLatestEventinfo. Here is my code snippet.

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

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

Or if their is another way to do so, kindly suggest me that.

推荐答案

远低于与通知工作,通过它,希望它可以帮助一个简单的例子.. !!

Well below is a simple example of working with Notifications, go through it, Hope it helps..!!

这是MainActivity.java ..

This is the 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

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" />

和将要开业点击通知的活动,

And the activity that will be opened on click of Notification,

public class SecActivity extends Activity{

}

这篇关于无法解析法setLatestEventInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 06:38