java - Android 收不到广播

标签 java android broadcastreceiver android-service

我正在从我的 Activity 中创建一个 IntentService 来执行一些后台工作,并且从该服务中,我正在广播接收一些数据的 Intent ,但未接收到广播。

这是我的类(class):

TwitterService.java

 public class TwitterService extends IntentService {

    public TwitterService() {
        super("TwitterService");
    }

    @Override
    protected void onHandleIntent(Intent workIntent) {
        Bundle bundle = workIntent.getExtras();

        List<twitter4j.Status> tweets = new ArrayList<twitter4j.Status>();

        Twitter twitter = TwitterFactory.getSingleton();

        try{
            tweets = twitter.search(new Query(bundle.getString("query").toString())).getTweets();
            Query query = new Query(bundle.getString("query").toString());
            query.resultType(Query.ResultType.recent);
            tweets = twitter.search(query).getTweets();

            System.out.println("results fetched");

            Intent test = new Intent();
            test.putExtra("a", "a");
            sendBroadcast(test);
        }
        catch (TwitterException e){
            System.out.println("Twitter Exception!");
            e.printStackTrace();
        }
    }
}

还有

TwitterReceiver.java

    public class TwitterReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle b = intent.getExtras();
        Log.d("Twitter Receiver", b.getString("a").toString());
    }
}

还有

AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.dhuzz.first" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name=".MainApp"
        android:debuggable="true" >
        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".HomeActivity"
            android:label="@string/app_name" >
        </activity>
        <activity android:name="com.facebook.FacebookActivity"
            android:configChanges=
                "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:label="@string/app_name">
        </activity>
        <service android:name=".TwitterService"
            android:exported="false"/>
        <receiver android:name=".TwitterReceiver">
            <intent-filter>
                <action android:name="TwitterBroadcast"/>
            </intent-filter>
        </receiver>
        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
    </application>

</manifest>

最佳答案

像这样更改广播 Intent 的代码

        Intent test = new Intent("TwitterBroadcast");
        test.putExtra("a", "a");
        sendBroadcast(test);

关于java - Android 收不到广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30752393/

相关文章:

java - 如何在Hybris中安装ywebfragmentcache?

java - 在 JSF 1.2 中创建动态 ENUM 菜单标签

java - DNS over TLS - 套接字关闭

android - 无法检测用户 Activity 转换,使用广播接收器

java - 从 BroadcastReceiver 对象访问类

java - 选中复选框后仅显示一次对话框

java - Tyk API Gateway 每 60 秒重试一次请求

Android - Bitmap.createScaledBitmap() 将 Config 设置为 ARGB_8888

java - 指定的子项已经有父项。我缺少什么

android - 屏幕关闭后无法保持传感器更新率