在状态通知上放置按钮

时间:2012-06-26 15:56:09

标签: java android oop user-interface

我想在状态栏中为我的通知添加两个按钮。当然,直到用户触摸扩展它们才会出现。我已经使用RemoteViews为我的通知创建了自定义布局,但由于我当前的代码结构,我不确定是否可以获取对它们的引用。

@Override
public void onMessage(Context context, Intent intent) {
            Log.w("C2DMReceiver",
            "Message Received, this is the message with no payload");
    Bundle extras = intent.getExtras();

    if (extras != null) {
        String[] payload = new String[3];
        payload[0] = (String) extras.get("payload");
        payload[1] = (String) extras.get("payload2");
        SharedPreferences sharedP = Prefs.get(this);
        boolean inApp = sharedP.getBoolean("currentlyInApp", true);
        if (!inApp) {
            createNotification(context, payload);
        }

    }
}

public void createNotification(Context context, String[] payload) {
    SharedPreferences sharedP = Prefs.get(context);
    boolean needsToLogin = sharedP
            .getBoolean("loginFromNotification", true);

    Log.w("C2DMReceiver", "createNotification called");

    NotificationManager notificationmanager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(this, WebViewActivity.class);
    Intent notificationIntent2 = new Intent(this, UniteActivity.class);
    PendingIntent pIntent;
    if (needsToLogin) {
        pIntent = PendingIntent.getActivity(this, 0, notificationIntent2,
                PendingIntent.FLAG_CANCEL_CURRENT);

    } else {
        pIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);
    }

    // Compatibility builder
    NotificationCompat.Builder notification = new NotificationCompat.Builder(
            context);
    RemoteViews remote = new RemoteViews(getPackageName(),R.layout.notification);


    //Button okButton = (Button) findViewById(R.layout.notification);

    notification.setAutoCancel(false);
    notification.setContent(remote);
    notification.setContentIntent(pIntent);
    notification.setWhen(System.currentTimeMillis());
    notification.setTicker(payload[0]);
    notification.setSmallIcon(R.drawable.default1);
    notification.setContentTitle(payload[1]);
    notification.setContentText(payload[0]);



    long duration[] = { 100, 300, 100 };
    notification.setVibrate(duration);

    notificationmanager.notify(0, notification.getNotification());
}

onMessage是一种从Google C2DM库中提取的方法,其中的通知是通过谷歌收到的意图生成的。没有视图,如何使用findViewById()获取对按钮的引用?或其他一些手段

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找方法:

RemoteViews.setOnClickPendingIntent(int, android.app.PendingIntent)


所以,如果你添加......

remote.setOnClickPendingIntent(R.id.button, pIntent);

......它应该有用。

相关问题