2015-06-10 239 views
1

我正在從我的活動IntentService做一些後臺工作,並從該服務,我廣播意圖接收一些數據,但廣播沒有收到。廣播沒有收到Android

這裏是我的課:

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> 
+0

廣播意圖的代碼,而你想要播放reciever? –

+0

我是Android的新手,我需要將服務中的數據發送回活動。我在線閱讀,Android文檔說使用廣播。 –

回答

0

更改你喜歡這個

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