0

我正在开发一个显示消息的应用程序,因为我在我的活动中使用 BroadcastReceiver 并且我正在尝试刷新 onReceive 方法中的列表视图,但是 onReceive 没有接到电话,请帮助我解决这个问题。

public class MessageActivity extends BaseActivity {


    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
            // Extract data included in the Intent
            String message = intent.getStringExtra("message");
            Log.d("receiver", "Got message: " + message);
            Toast.makeText(getApplicationContext(), "Inside Reciver",Toast.LENGTH_SHORT).show();
            getSMSCursorCount();
            loadInitialMessages();
          }
        };
protected void onCreate(Bundle savedInstanceState) {

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.messageinbox);
}

    @Override
    public void onResume() {
        super.onResume();
        IntentFilter intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
        intentFilter.setPriority(1000);
        registerReceiver(mMessageReceiver, intentFilter);
            Log.d("This is onResume method ","<<<<<<<<< onResume <<<<<<<<<");
            try {
                getSMSCursorCount();
                loadInitialMessages();
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        try {
            unregisterReceiver(mMessageReceiver);
        } catch (IllegalArgumentException e) {
            if (e.getMessage().contains("Receiver not registered")) {
                // Ignore this exception. This is exactly what is desired
                Log.w(TAG,"Tried to unregister the reciver when it's not registered");
            } else {
                // unexpected, re-throw
                throw e;
            }
        }
         Log.d("This is onPause method ","<<<<<<<<< onPause <<<<<<<<<");
    }
4

1 回答 1

1

您必须通过扩展 BroadcastReceiver 来编写单独的广播接收器类,并使用 intentfilter 在清单文件中声明它。覆盖 onReceive 方法并在那里调用您的活动。如果意图来自广播接收器,则在活动的 oncreate 中刷新您的列表视图。

 public class MessageReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("inside message receiver... ");

           Intent popupintent = new Intent(context,ShowNotificationDialog.class);
            popupintent.putExtra("from", "broadcast");
            context.startActivity(popupintent);  



    }



public class ShowNotificationDialog extends Activity
        {

         @Override
            protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       Intent i = getIntent();
      String fromString = i.getStringExtra("from");
      if(from != null)
       {
       if(from.equals("from"))

       // refresh your listview

       }

    }
}

在清单中:

<receiver android:name=".MessageReceiver" >
            <intent-filter>
                <action android:name="broadcast action" />
            </intent-filter>
        </receiver>
于 2013-09-17T07:32:14.667 回答