Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

android - Adding onClick Action To Button In Notification

I'm trying to add button to Notification using custom layout. I was able to add the layout and display the button. However, I still can't figure out a way to add click listener to the button. Here is the relevant code I have:

Codes to add custom layout notification:

String ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) ctx.getSystemService(ns);
CharSequence tickerText = "Quick Application Launcher";
long when = System.currentTimeMillis();
Notification.Builder builder = new Notification.Builder(ctx);
Notification notification=builder.getNotification();
notification.when=when;
notification.tickerText=tickerText;
notification.icon=R.drawable.ic_launcher;

RemoteViews contentView=new RemoteViews(ctx.getPackageName(), R.layout.custom_notification);

Intent volume=new Intent(ctx, NotifActivityHandler.class);
volume.putExtra("DO", "2");
PendingIntent pVolume = PendingIntent.getActivity(ctx, 1, volume, 0);
contentView.setOnClickPendingIntent(R.id.btn2, pVolume);

notification.contentView = contentView;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(2345345, notification);

This is the NotifActivityHandler code:

public class NotifActivityHandler extends Activity {

    private NotifActivityHandler ctx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ctx=this;
        String action= (String)getIntent().getExtras().get("DO");
        Log.i("LOG", "lauching action: " + action);
        if(action.equals("1")){
        } else if(action.equals("2")){
        } else if(action.equals("config")){
            Intent i = new Intent(NotifActivityHandler.this, ConfigActivity.class);
            startActivity(i);
        }
    }   
}

Codes above doesn't produce any log even if I put Log.i. I'm not sure what is wrong with this. Any help is appreciated.

update

I tested this on ICS device.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You may have to register your listner in AndroidManifest.xml. Take a look at this one


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...