Android 状态栏通知操作/回调

Android 状态栏通知操作/回调,android,cordova,Android,Cordova,我正在试图弄清楚,当通知是选项卡式的时,如何添加一个处理程序/操作,这是我到目前为止的代码 package com.phonegap.plugins.SystemNotification; import org.json.JSONArray; import org.json.JSONException; import android.app.Notification; import android.app.NotificationManager; import android.app.Pend

我正在试图弄清楚,当通知是选项卡式的时,如何添加一个处理程序/操作,这是我到目前为止的代码

package com.phonegap.plugins.SystemNotification;

import org.json.JSONArray;
import org.json.JSONException;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import org.json.JSONArray;

public class SystemNotification extends Plugin {

    final int notif_ID = 1234;
    NotificationManager notificationManager;
    Notification note;
    PendingIntent contentIntent;

    @Override
    public PluginResult execute(String action, JSONArray args, String callbackId)
    {

        PluginResult.Status status = PluginResult.Status.OK;
        String result = "";

        try {
            if (action.equals("createStatusBarNotification")) {
                this.createStatusBarNotification(args.getString(0), args.getString(1), args.getString(2));
            }
            else if (action.equals("updateNotification")) {
                this.updateNotification(args.getString(0), args.getString(1), args.getInt(2));
            }
            else if (action.equals("cancelNotification")) {
                this.cancelNotification();
            }
            else if (action.equals("showTickerText")) {
                this.showTickerText(args.getString(0));
            }
            return new PluginResult(status, result);
        } catch(JSONException e) {
            return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
        }
    }

    private void updateNotification(String contentTitle, String contentText, int number)
    {
        note.setLatestEventInfo(this.ctx, contentTitle, contentText, contentIntent);
        note.number = number;
        notificationManager.notify(notif_ID,note);
    }

    private void createStatusBarNotification(String contentTitle, String contentText, String tickerText)
    {
        notificationManager = (NotificationManager) this.ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        note = new Notification(android.R.drawable.btn_star_big_on, tickerText, System.currentTimeMillis() );
    //change the icon

    Intent notificationIntent = new Intent(this.ctx, SystemNotification.class);
        notificationIntent.setAction(Intent.ACTION_MAIN);
        notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        contentIntent = PendingIntent.getActivity(this.ctx, 0, notificationIntent, 0);

        note.defaults |= Notification.DEFAULT_SOUND;
        note.defaults |= Notification.DEFAULT_VIBRATE;
        note.defaults |= Notification.DEFAULT_LIGHTS;
        note.defaults |= Notification.FLAG_AUTO_CANCEL;

        note.ledARGB = 0xff00ff00;
        note.ledOnMS = 300;
        note.ledOffMS = 1000;
        note.flags |= Notification.FLAG_SHOW_LIGHTS;

        note.setLatestEventInfo(this.ctx, contentTitle, contentText, contentIntent);

        note.number = 1;  //Just created notification so number=1. Remove this line if you dont want numbers

        notificationManager.notify(notif_ID,note);
    }

    private void cancelNotification()
    {
        notificationManager.cancel(notif_ID);
    }

    private void showTickerText(String tickerText)
    {
        note.tickerText = tickerText;
        notificationManager.notify(notif_ID,note);
    }

    @Override
    public void onPause()
    {
        super.webView.loadUrl("javascript:window.plugins.SystemNotification.onBackground();");
    }

    @Override
    public void onResume()
    {
        super.webView.loadUrl("javascript:window.plugins.SystemNotification.onForeground();");
    }

}
代码来自 我做错了什么?当我点击通知时,现在什么都没有发生

编辑//androidmanifest:

    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".HelloAndroid"
              android:label="@string/app_name"
              android:configChanges="orientation|keyboardHidden"
              android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

因为我正在使用phonegap,所以我需要更改类的意图。在我的情况下,那是他自己的问题。这是在Droidgap上延伸的一个

Intent notificationIntent=新意图(this.ctx,SystemNotification.class)

Intent notificationIntent=新的Intent(this.ctx,HelloAndroid.class)


谢谢大家的帮助

您是否确实有一个名为
SystemNotification
的活动,并且它也在您的
AndroidManifest
中?请参阅第一篇文章中的AndroidManifest。我真的不知道这一切是怎么回事works@user706933:FWIW,下面是一个使用
通知
:@commonware Thnx作为答案的简单示例。我已经研究过了,在您的示例中,当通知被标记时,您没有使用任何操作。eclipse中的logcat说:窗口已经聚焦,忽略:com…@user706933的聚焦增益:“通知是选项卡式的”是什么意思?在
通知中不能有
TabHost
或可聚焦的小部件。