【发布时间】:2011-05-01 15:44:58
【问题描述】:
我正试图弄清楚如何在通知被选项卡时添加处理程序/操作,这是我到目前为止的代码。
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();");
}
}
代码来自https://github.com/saileshmittal/phonegap-system-notification-plugin 我究竟做错了什么?当我点击通知时,现在什么都没有发生。
编辑// 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>
【问题讨论】:
-
你真的有一个名为
SystemNotification的活动吗?它也在你的AndroidManifest中吗? -
查看 androidmanifest 的第一篇文章。我实际上不知道这一切是如何运作的
-
@user706933:FWIW,这是一个使用
Notification的更简单示例:github.com/commonsguy/cw-android/tree/master/Notifications/… -
@CommonsWare Thnx 为您解答。我已经调查过了,在你的例子中,当通知被标签时,你没有使用任何操作。日食中的 logcat 说:窗口已经聚焦,忽略焦点增益:com....
-
@user706933:你所说的“通知是标签式的”是什么意思?
Notification中不能有TabHost或可聚焦小部件。