我正在开发一个显示消息的应用程序,因为我在我的活动中使用 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 <<<<<<<<<");
}