简体   繁体   English

BroadCastReceiver Android

[英]BroadCastReceiver Android

I'm realy newbie in android, and i'm having dificult with broadcastreceiver in my project. 我真的是android的新手,我在我的项目中与broadcastreceiver有分歧。

I've created uses-permission and the receiver in AndroidManifest.xml, created the class but it not passing on the logcat message. 我在AndroidManifest.xml中创建了uses-permission和接收器,创建了该类,但未传递logcat消息。

MY RECEIVER CLASS 我的接收器类

package com.polifrete.polifreteFunctions;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.i("Script", "Passou AQUI");
        Intent i = new Intent(context, VerificaNovoFreteService.class);
        context.startService(i);
    }
}

MY ANDROIDMANIFEST.XML 我的ANDROIDMANIFEST.XML

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <receiver android:name="com.polifrete.polifreteFunctions.MyReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
    </receiver>

What i'm doing wrong? 我做错了什么? Please, help me! 请帮我!

Not every Phone sends the BOOT_COMPLETED Broadcast. 并非每个电话都发送BOOT_COMPLETED广播。 For example, I think it was HTC, sends the QUICKBOOT_POWERON Broadcast, if it has boot up. 例如,我认为是HTC,如果已启动,则发送QUICKBOOT_POWERON Broadcast。 Also your logcat is maybe not connected to your phone at Boot-Up-Time. 另外,您的logcat可能在启动时未连接到手机。 I'd suggest to create a Toast to check, wether your Boot-Reciever is executed. 我建议创建一个Toast来检查,是否执行了Boot-Reciever

You can create this Toast with this code: 您可以使用以下代码创建此Toast

Toast toast = Toast.makeText(getApplicationContext(), "Boot-Reciever Started", Toast.LENGT_LONG);

@Felipe A., it's possible that you have a problems with CPU...The oficial documentation talks about it, and says the best use is WakefulBroadcastReceiver guarantee a that CPU will stays awake, you can read this post that talk about it: https://stackoverflow.com/a/26380942/3996257 @Felipe A.,您的CPU可能有问题...官方文档对此进行了讨论,并说最好的用法是WakefulBroadcastReceiver确保CPU保持清醒状态,您可以阅读有关此内容的文章: https ://stackoverflow.com/a/26380942/3996257

This is my code with WakefulBroadcastReceiver: 这是我的WakefulBroadcastReceiver代码:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),
            GcmIntent_Service.class.getName());
        // Start the service, keeping the device awake while it is launching.
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

The gcm receiver needs to add your package in your intent-filter it's the only difference with my code. gcm接收器需要将您的包添加到您的intent过滤器中,这与我的代码唯一不同。 And my receiver have this code: 我的接收器有以下代码:

    <receiver
        android:name=".App_Services.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />/>
            <category android:name="your.package" />
        </intent-filter>
    </receiver>

It's possible your code don't recognizes your receiver because doesn't know their package. 您的代码可能由于不知道接收者的包裹而无法识别接收者。 Tell me if I helps you, good luck! 告诉我,如果我有帮助,祝您好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM