我正在开发一个显示消息的应用程序,因为我在我的活动中使用BroadcastReceiver而我正在尝试刷新onReceive方法中的listview,但onReceive没有得到一些请求如何帮助我。< / p>
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 <<<<<<<<<");
}
答案 0 :(得分:1)
您必须通过扩展BroadcastReceiver来编写单独的广播接收器类,并使用intentfilter在清单文件中声明它。覆盖onReceive方法并在那里调用你的活动。如果意图来自广播接收者,则在oncreate of activity刷新你的listview。
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>