【问题标题】:GCM wakelock permissionGCM 唤醒锁权限
【发布时间】:2015-10-14 20:52:12
【问题描述】:

在我看到谷歌关于性能模式的视频 - video 后,我决定实施谷歌云消息传递。 但是,当我为 Android 实现 GCM 时,我看到 GCM 正在使用一些权限,其中之一是“wakelock”。 但正如我们所知,此权限“等于”耗电。

所以我的问题是,我们如何处理这个问题? Lib GCM 为我们做这个?使用拉取通知比拉取更好吗?

谢谢

【问题讨论】:

  • 但正如我们所知,此权限“等于”耗电。 不,它不是...如果 我们 这可能是真的i>等于不知道怎么回事的人
  • 我们知道 gcm 正在使用广播...我们知道 CPU 仅在 onReceive 执行时唤醒...我们知道 onReceive 应该几乎立即返回...这就是 gcm 启动唤醒服务 GcmListenerService 的原因...在 onMessageReceived 完成后立即释放锁定...因此,电池消耗仅取决于您。如果您将在 onMessageReceived 中执行非常长时间的操作,那么电池将会耗尽......如果没有,那么它会像(几乎)那样运行,根本不会有 CPU 锁定。

标签: android google-cloud-messaging


【解决方案1】:

你不必这样做。谷歌提供了一项为您处理一切的服务。如下:

 public class GcmListenerService extends com.google.android.gms.gcm.GcmListenerService {

private static final String TAG = "GcmListenerService";

/**
 * Called when message is received.
 *
 * @param from SenderID of the sender.
 * @param data Data bundle containing message data as key/value pairs.
 *             For Set of keys use data.keySet().
 */
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("message");
    Log.d(TAG, "From: " + from);
    Log.d(TAG, "Message: " + message);

    if (from.startsWith("/topics/")) {
        // message received from some topic.
    } else {
        // normal downstream message.
    }

    // [START_EXCLUDE]
    /**
     * Production applications would usually process the message here.
     * Eg: - Syncing with server.
     *     - Store message in local database.
     *     - Update UI.
     */

    /**
     * In some cases it may be useful to show a notification indicating to the user
     * that a message was received.
     */
    sendNotification(message);
    // [END_EXCLUDE]
}
// [END receive_message]

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received.
 */
private void sendNotification(String message) {
    Intent intent = new Intent(this, TabAllItemsActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, Resources.GCM_NOTIFICATION_REQUEST_CODE, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_notification)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.icon_app))
            .setContentTitle("Message from Faroque")
            .setContentText(message)
            .setColor(R.color.orange_dark)//setting the brand color
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);//This intent will be executed when user tap on it

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(Resources.GCM_NOTIFICATION_ID, notificationBuilder.build());
}

}

Here是教程,需要的可以查看。谢谢

【讨论】:

  • 感谢您的回答和教程。但这并不能解决我关于唤醒锁的问题
  • 你不需要唤醒锁。唤醒锁用于在 CPU 处于睡眠状态时激活它。为此,您需要一个广播接收器。但我给你的这项服务,它会处理一切。从github.com/nanofaroque/GoogleCloudMessagingApp 运行代码,看看它是如何工作的。谢谢
【解决方案2】:

定义唤醒锁权限本身不会导致电池耗尽。滥用唤醒锁确实如此。 GcmListenerService 为您管理唤醒锁,这是使用 GcmListenerService 是个好主意的原因之一,因为您不必自己管理唤醒锁(这很重要)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多