- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试让 C2DM 工作,我正在关注 ChrometoPhone 示例 here当我的模拟器尝试注册到 C2DM 时,我得到 SERVICE_NOT_AVAILABLE
。我已检查模拟器是否设置了 gmail 帐户,并且在示例代码中,同一帐户作为 senderId
从 DeviceRegistrar.java
传递。我也确信我的模拟器可以访问互联网并且我拥有所有权限(访问互联网、唤醒锁等)。这里还有什么问题?
我不确定我是否很好地解释了问题。如果我需要解释其他事情,请告诉我。
这里是C2DMBaseReceiver.java
package com.apps.terrapin;
import java.io.IOException;
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;
/**
* Base class for C2D message receiver. Includes constants for the
* strings used in the protocol.
*/
public abstract class C2DMBaseReceiver extends IntentService {
private static final String C2DM_RETRY = "com.google.android.c2dm.intent.RETRY";
public static final String REGISTRATION_CALLBACK_INTENT = "com.google.android.c2dm.intent.REGISTRATION";
private static final String C2DM_INTENT = "com.google.android.c2dm.intent.RECEIVE";
// Logging tag
private static final String TAG = "C2DM";
// Extras in the registration callback intents.
public static final String EXTRA_UNREGISTERED = "unregistered";
public static final String EXTRA_ERROR = "error";
public static final String EXTRA_REGISTRATION_ID = "registration_id";
public static final String ERR_SERVICE_NOT_AVAILABLE = "SERVICE_NOT_AVAILABLE";
public static final String ERR_ACCOUNT_MISSING = "ACCOUNT_MISSING";
public static final String ERR_AUTHENTICATION_FAILED = "AUTHENTICATION_FAILED";
public static final String ERR_TOO_MANY_REGISTRATIONS = "TOO_MANY_REGISTRATIONS";
public static final String ERR_INVALID_PARAMETERS = "INVALID_PARAMETERS";
public static final String ERR_INVALID_SENDER = "INVALID_SENDER";
public static final String ERR_PHONE_REGISTRATION_ERROR = "PHONE_REGISTRATION_ERROR";
// wakelock
private static final String WAKELOCK_KEY = "C2DM_LIB";
private static PowerManager.WakeLock mWakeLock;
private final String senderId;
/**
* The C2DMReceiver class must create a no-arg constructor and pass the
* sender id to be used for registration.
*/
public C2DMBaseReceiver(String senderId) {
// senderId is used as base name for threads, etc.
super(senderId);
this.senderId = senderId;
}
/**
* Called when a cloud message has been received.
*/
protected abstract void onMessage(Context context, Intent intent);
/**
* Called on registration error. Override to provide better
* error messages.
*
* This is called in the context of a Service - no dialog or UI.
*/
public abstract void onError(Context context, String errorId);
/**
* Called when a registration token has been received.
*/
public void onRegistered(Context context, String registrationId) throws IOException {
// registrationId will also be saved
}
/**
* Called when the device has been unregistered.
*/
public void onUnregistered(Context context) {
}
@Override
public final void onHandleIntent(Intent intent) {
try {
Context context = getApplicationContext();
if (intent.getAction().equals(REGISTRATION_CALLBACK_INTENT)) {
handleRegistration(context, intent);
} else if (intent.getAction().equals(C2DM_INTENT)) {
onMessage(context, intent);
} else if (intent.getAction().equals(C2DM_RETRY)) {
C2DMessaging.register(context, senderId);
}
} finally {
// Release the power lock, so phone can get back to sleep.
// The lock is reference counted by default, so multiple
// messages are ok.
// If the onMessage() needs to spawn a thread or do something else,
// it should use it's own lock.
mWakeLock.release();
}
}
/**
* Called from the broadcast receiver.
* Will process the received intent, call handleMessage(), registered(), etc.
* in background threads, with a wake lock, while keeping the service
* alive.
*/
static void runIntentInService(Context context, Intent intent) {
if (mWakeLock == null) {
// This is called from BroadcastReceiver, there is no init.
PowerManager pm =
(PowerManager) context.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
WAKELOCK_KEY);
}
mWakeLock.acquire();
// Use a naming convention, similar with how permissions and intents are
// used. Alternatives are introspection or an ugly use of statics.
String receiver = context.getPackageName() + ".C2DMReceiver";
intent.setClassName(context, receiver);
context.startService(intent);
}
private void handleRegistration(final Context context, Intent intent) {
final String registrationId = intent.getStringExtra(EXTRA_REGISTRATION_ID);
String error = intent.getStringExtra(EXTRA_ERROR);
String removed = intent.getStringExtra(EXTRA_UNREGISTERED);
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "dmControl: registrationId = " + registrationId +
", error = " + error + ", removed = " + removed);
}
if (removed != null) {
// Remember we are unregistered
C2DMessaging.clearRegistrationId(context);
onUnregistered(context);
return;
} else if (error != null) {
// we are not registered, can try again
C2DMessaging.clearRegistrationId(context);
// Registration failed
Log.e(TAG, "Registration error " + error);
onError(context, error);
if ("SERVICE_NOT_AVAILABLE".equals(error)) {
long backoffTimeMs = C2DMessaging.getBackoff(context);
Log.d(TAG, "Scheduling registration retry, backoff = " + backoffTimeMs);
Intent retryIntent = new Intent(C2DM_RETRY);
PendingIntent retryPIntent = PendingIntent.getBroadcast(context,
0 /*requestCode*/, retryIntent, 0 /*flags*/);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.ELAPSED_REALTIME,
backoffTimeMs, retryPIntent);
// Next retry should wait longer.
backoffTimeMs *= 2;
C2DMessaging.setBackoff(context, backoffTimeMs);
}
} else {
try {
onRegistered(context, registrationId);
C2DMessaging.setRegistrationId(context, registrationId);
} catch (IOException ex) {
Log.e(TAG, "Registration error " + ex.getMessage());
}
}
}
}
C2DMBBroadcastReceiver.java
package com.apps.terrapin;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* Helper class to handle BroadcastReciver behavior.
* - can only run for a limited amount of time - it must start a real service
* for longer activity
* - must get the power lock, must make sure it's released when all done.
*
*/
public class C2DMBroadcastReceiver extends BroadcastReceiver {
@Override
public final void onReceive(Context context, Intent intent) {
// To keep things in one place.
C2DMBaseReceiver.runIntentInService(context, intent);
setResult(Activity.RESULT_OK, null /* data */, null /* extra */);
if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
Log.e("TAG", "Broadcast receiver got REGISTRATION");
Log.e("TAG", "ID: " + intent.getExtras().getString("registration_id"));
} else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
Log.e("TAG", "Broadcast receiver got message");
}
}
}
C2DMessaging.java
package com.apps.terrapin;
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
/**
* Utilities for device registration.
*
* Will keep track of the registration token in a private preference.
*/
public class C2DMessaging {
public static final String EXTRA_SENDER = "sender";
public static final String EXTRA_APPLICATION_PENDING_INTENT = "app";
public static final String REQUEST_UNREGISTRATION_INTENT = "com.google.android.c2dm.intent.UNREGISTER";
public static final String REQUEST_REGISTRATION_INTENT = "com.google.android.c2dm.intent.REGISTER";
public static final String LAST_REGISTRATION_CHANGE = "last_registration_change";
public static final String BACKOFF = "backoff";
public static final String GSF_PACKAGE = "com.google.android.gsf";
// package
static final String PREFERENCE = "com.google.android.c2dm";
private static final long DEFAULT_BACKOFF = 30000;
/**
* Initiate c2d messaging registration for the current application
*/
public static void register(Context context, String senderId) {
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
//registrationIntent.setPackage(GSF_PACKAGE);
registrationIntent.putExtra("app", PendingIntent.getBroadcast(context, 0, new Intent(), 0));
registrationIntent.putExtra("sender", senderId);
context.startService(registrationIntent);
}
/**
* Unregister the application. New messages will be blocked by server.
*/
public static void unregister(Context context) {
Intent regIntent = new Intent(REQUEST_UNREGISTRATION_INTENT);
regIntent.setPackage(GSF_PACKAGE);
regIntent.putExtra(EXTRA_APPLICATION_PENDING_INTENT, PendingIntent.getBroadcast(context,
0, new Intent(), 0));
context.startService(regIntent);
}
/**
* Return the current registration id.
*
* If result is empty, the registration has failed.
*
* @return registration id, or empty string if the registration is not complete.
*/
public static String getRegistrationId(Context context) {
final SharedPreferences prefs = context.getSharedPreferences(
PREFERENCE,
Context.MODE_PRIVATE);
String registrationId = prefs.getString("dm_registration", "");
return registrationId;
}
public static long getLastRegistrationChange(Context context) {
final SharedPreferences prefs = context.getSharedPreferences(
PREFERENCE,
Context.MODE_PRIVATE);
return prefs.getLong(LAST_REGISTRATION_CHANGE, 0);
}
static long getBackoff(Context context) {
final SharedPreferences prefs = context.getSharedPreferences(
PREFERENCE,
Context.MODE_PRIVATE);
return prefs.getLong(BACKOFF, DEFAULT_BACKOFF);
}
static void setBackoff(Context context, long backoff) {
final SharedPreferences prefs = context.getSharedPreferences(
PREFERENCE,
Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putLong(BACKOFF, backoff);
editor.commit();
}
// package
static void clearRegistrationId(Context context) {
final SharedPreferences prefs = context.getSharedPreferences(
PREFERENCE,
Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString("dm_registration", "");
editor.putLong(LAST_REGISTRATION_CHANGE, System.currentTimeMillis());
editor.commit();
}
// package
static void setRegistrationId(Context context, String registrationId) {
final SharedPreferences prefs = context.getSharedPreferences(
PREFERENCE,
Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString("dm_registration", registrationId);
editor.commit();
}
}
C2DMReceiver.java
package com.apps.terrapin;
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
public class C2DMReceiver extends C2DMBaseReceiver {
public C2DMReceiver() {
super(DeviceRegistrar.SENDER_ID);
}
@Override
public void onRegistered(Context context, String registration) {
DeviceRegistrar.registerWithServer(context, registration);
}
@Override
public void onUnregistered(Context context) {
SharedPreferences prefs = Prefs.get(context);
String deviceRegistrationID = prefs.getString("deviceRegistrationID",
null);
DeviceRegistrar.unregisterWithServer(context, deviceRegistrationID);
}
@Override
public void onError(Context context, String errorId) {
Log.e("TAG", "Error ocurred in onError");
}
@Override
public void onMessage(Context context, Intent intent) {
Bundle extras = intent.getExtras();
String msg = extras.getString("message");
Log.e("TAG", "Got a message from cloud: " + msg);
}
}
DeviceRegistrar.java
package com.apps.terrapin;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.content.Context;
import android.util.Log;
public class DeviceRegistrar {
static final String SENDER_ID = "sender@gmail.com";
public static void registerWithServer(final Context context, final String deviceRegistrationID) {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
String url = "http://" + "192.168.1.11" + "/message.php?";
url += "device=" + deviceRegistrationID;
try {
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
Log.d("@@@@@ Server response @@@@@", response.toString());
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void unregisterWithServer(final Context context, final String deviceRegistrationID) {
}
}
list :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="preferExternal"
package="com.apps.terrapin"
android:versionCode="1"
android:versionName="1.0" >
<!-- Only this application can receive the messages and registration result -->
<permission android:name="com.apps.terrapin.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.apps.terrapin.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Permissions for internet access and account access -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- App must have this permission to use the library -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:icon="@drawable/earth"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".TerraPin" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TakePictureActivity" />
<activity android:name=".SearchActivity" />
<activity android:name=".SettingsActivity" />
<uses-library android:name="com.google.android.maps" />
<service android:name=".C2DMReceiver" />
<!-- Only google service can send data messages for the app. If permission is not set -
any other app can generate it -->
<receiver android:name="com.apps.terrapin.C2DMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<!-- Receive the actual message -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.apps.terrapin" />
</intent-filter>
<!-- Receive the registration id -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.apps.terrapin" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
</manifest>
返回的错误是SERVICE_NOT_AVAILABLE
在我的主要 Activity 中,我这样做:
C2DMessaging.register(this, DeviceRegistrar.SENDER_ID);
String regId = C2DMessaging.getRegistrationId(this);
if (regId != null && !"".equals(regId)) {
DeviceRegistrar.registerWithServer(this, regId);
} else {
C2DMessaging.register(this, DeviceRegistrar.SENDER_ID);
}
最佳答案
在您的 Android 虚拟设备管理器中编辑您的 Android 虚拟设备 (AVD) 并设置目标“Google API (Google Inc.) - API Level 8”而不是“Android 2.2 - API Level 8”。完成后运行您的模拟器并在设置中设置您的谷歌帐户(输入白名单电子邮件)。
这解决了我的问题。
关于java - 使用 c2dm 时出现 SERVICE_NOT_AVAILABLE 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8460854/
通过反复试验,我发现GetPixel函数接受两个参数,一个用于 X,一个用于 Y,即使用于一维图像。在 1D 图像上,第二个索引必须设置为零。 image list := [3]: {1,2,3} l
除了 Funambol 或实现 OMA DM 的库之外,您是否知道任何 OMA DM 服务器的开源实现? 最佳答案 您可能想尝试一下OMA-DM simulator由 Koneki(Eclipse 开
我想计算给定ROI的曲线下面积,如附图,我想得到13到30的绿色曲线下面积,能否通过DM脚本实现这个功能? 以前从未做过类似的事情,任何建议谢谢! 最佳答案 如果图像是标签“A”,那么您可以简单地执行
我有两个模型:论坛应用程序中的帖子和图像,其中帖子使用 dm-is-tree 以父子格式排列。到目前为止,图像是 Post 模型的一部分。由于 Post 模型变得笨拙,我需要增加更多深度来标记图像,我
全部:我想构造一个函数。返回值的类型可能有两种可能:数字或字符串。起初,我以为可以使用重载函数(参见代码)。但这个函数必须有不同类型的输入。所以下面的代码会出错。有没有可行且简单的方法来解决这个问题?
要在 SAS 中打开表,我们可以使用 DM“VT {tablename}”命令。 关闭可视窗口的等效命令是什么??? 最佳答案 嗯...这个命令将关闭可视化窗口(用你的数据替换 {libref.dat
我正在尝试抓取我的 Instagram DM(直接消息)。但是,我不知道如何自动向下滚动该列以获取向我发送消息的其他用户名。我试过了 this ,以及滚动代码listed here . 但是,两者都不
我对 Blueprint 和 Spring DM 有点困惑: 据我认为的真实情况: Spring DM 是 Spring Source 定义的框架 Blueprint 是 OSGi 联盟定义的框架 B
是否有任何程序或任何控制台脚本可以监听麦克风输入,并且当噪音超过 db 中的某个阈值时,会播放音频文件? 最佳答案 需要考虑的一件事是,当使用 sox(例如使用 rec)录制时,sox 会输出一个文本
我正在尝试为我的机器人创建一个 VA 培训部分,它应该向我发送他们要求的培训类型。但是我碰壁了,不知道从这里该往哪里走。 我在网上寻找解决方案,但没有找到适合我的解决方案 if (cmd === `$
我开始研究 osgi 的主要目的是完成我面前的任务。基本上我将能够分发基于 Web 的应用程序并单独构建整个 Web 应用程序的特定功能,这样我就可以在 A 部署具有功能 a、b、c 的 Web 应用
vbs调用插件报:ActiveX部件不能创建对象,代码:800A01AD 报错如下图: 解决方法需要两步: 第一步改注册表: HKEY_CLASSES_ROOT\VBSFile\S
我正在寻找一个非常基本的配置示例。我有一个wap设置文件,我只想使用它来配置设备。 我正在寻找具有此功能的示例的API? 最佳答案 尝试S60 Platform: OMA Client Provisi
例如,当有人私信我的机器人时,它会说“你好”或“你为什么要私信机器人......?”我用这段代码试过了: @client.event async def on_message(message: dis
为了在SI图像中进行漂移校正,如下图所示: 我写代码: number max_shift=5 image src := GetFrontImage() number sx, sy, sz src.Ge
试图让我的机器人回复私信“这是一个私信”,但没有成功,这是我的代码: @client.event async def on_message(message): if message.guild
试图让我的机器人回复私信“这是一个私信”,但没有成功,这是我的代码: @client.event async def on_message(message): if message.guild
我需要等待一段时间才能关闭 OSGI 上下文。(给一些时间来完成当前正在运行的任务)。我在 extenderProperties bean 中遇到了 shutdown.wait.time 属性。 谁能
我在向特定人员发送 PM 时遇到了一些问题。我知道如何向消息的作者发送 DM。但是,我希望尝试将 DM 直接发送给特定的人。 async run(message, args) { if (ar
按照 Heroku 网站上的说明,我安装了 Postgress.app,安装了 pg gem,安装了 data_mapper,一切都很顺利。最后,尝试安装 dm-sqlite-adapter 并得到这
我是一名优秀的程序员,十分优秀!