2016-03-02 2 views
1

Приветствую всех, я следую примеру приложения чата по ссылке? и -Получить метод не будучи called.I хотите List View, но есть какие-либо проблемы регистрации кода receiver.My являетсяПриложение для Android чата - Проблема с широковещательным приемником

public class UserListActivity extends ListActivity { 
    private static final String TAG = "UserListActivity"; 
    TextView content; 
    Button refreshButton; 
    private Intent intent; 
    MessageSender messageSender; 
    GoogleCloudMessaging gcm; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_user_list); 
     content = (TextView)findViewById(R.id.output); 
     content.setText("Select user to chat:"); 
     refreshButton = (Button)findViewById(R.id.refreshButton); 
     intent = new Intent(this, GCMNotificationIntentService.class); 
     registerReceiver(broadcastReceiver, 
     newIntentFilter("com.javapapers.android.gcm.chat.userlist")); 
     messageSender = new MessageSender(); 
     gcm = GoogleCloudMessaging.getInstance(getApplicationContext()); 
     refreshButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // get user list 
       Bundle dataBundle = new Bundle(); 
       dataBundle.putString("ACTION", "USERLIST"); 
       messageSender.sendMessage(dataBundle, gcm); 
      } 
     }); 
    } 

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      Log.d(TAG, "onReceive: " + intent.getStringExtra("USERLIST")); 
      updateUI(intent.getStringExtra("USERLIST")); 
     } 
    }; 

    private void updateUI(String userList) { 
     //get userlist from the intents and update the list 
     String[] userListArr = userList.split(":");  
     Log.d(TAG,"userListArr: "+userListArr.length+" 
     tostr"+userListArr.toString()); 
     //remove empty strings :-) 
     List<String> list = new ArrayList<String>(); 
     for(String s : userListArr) { 
      if(s != null && s.length() > 0) { 
       list.add(s); 
      } 
     } 
     userListArr = list.toArray(new String[list.size()]); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
     android.R.layout.simple_list_item_1, userListArr); 
     setListAdapter(adapter); 
    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id){ 
      // ListView Clicked item index 
      int itemPosition  = position;  
      // ListView Clicked item value 
      String itemValue = (String) l.getItemAtPosition(position); 
      content.setText("User selected: " +itemValue);  
      Intent i = new Intent(getApplicationContext(), 
      ChatActivity.class); 
      i.putExtra("TOUSER",itemValue); 
      startActivity(i); 
      finish(); 
     }  

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) {   
      // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.user_list, menu); 
      return true; 
     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      // Handle action bar item clicks here. The action bar will 
      // automatically handle clicks on the Home/Up button, so long 
      // as you specify a parent activity in AndroidManifest.xml. 
      int id = item.getItemId(); 
      if (id == R.id.action_settings) { 
       return true; 
      } 
      return super.onOptionsItemSelected(item); 
     } 

     @Override 
     public void onBackPressed() { 
      // TODO Auto-generated method stub 
      super.onBackPressed(); 
     } 

     @Override 
     protected void onStop() { 
      // TODO Auto-generated method stub 
      unregisterReceiver(broadcastReceiver); 
      super.onDestroy(); 
     } 
    } 
} 

И мой manifestfile.xml ниже

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mychat" android:versionCode="1" android:versionName="1.0" > 
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> 
    <uses-permission android:name="com.google.android.c2dm.permission. RECEIVE"  /> 
    <uses-permission android:name="app.cloudstringers.permission.C2D_MESSAGE" /> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 

    <permission android:name="app.cloudstringers.permission.C2D_MESSAGE" android:protectionLevel="signature" /> 

    <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > 
     <activity android:name=".SplashActivity" 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="ChatActivity" ></activity> 
     <activity android:name="SignUpActivity" ></activity> 
     <activity android:name="UserListActivity" ></activity> 
     <receiver android:name="com.example.mychat.GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
       <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
       <category android:name="com.example.mychat" /> 
      </intent-filter> 
     </receiver> 
     <service android:name="com.example.mychat.GCMNotificationIntentService" /> 
     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> 
    </application> 
</manifest> 
+0

регистра приемника в onResume и UNREGISTER в OnPause –

+0

и откуда вы отправляете Broadcast –

+0

Просто убедитесь, что вы правильно транслирует намерения действия «com.javapapers.android.gcm.chat.userlist» –

ответ

0

ли вы пропустили вызов unregisterReceiver()?

Добавить эту строку unregisterReceiver (broadcastReceiver);

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_user_list); 
    content = (TextView)findViewById(R.id.output); 
    content.setText("Select user to chat:"); 
    refreshButton = (Button)findViewById(R.id.refreshButton); 
    intent = new Intent(this, GCMNotificationIntentService.class); 
    registerReceiver(broadcastReceiver, new IntentFilter("com.javapapers.android.gcm.chat.userlist")); 
    messageSender = new MessageSender(); 
    gcm = GoogleCloudMessaging.getInstance(getApplicationContext()); 
    refreshButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      // get user list 
      Bundle dataBundle = new Bundle(); 
      dataBundle.putString("ACTION", "USERLIST"); 
      messageSender.sendMessage(dataBundle, gcm); 
     } 
    }); 
    unregisterReceiver(broadcastReceiver); 
} 
Смежные вопросы