我最近有一个问题(Sharing data between activitiesin Android),我解决了部分问题,但我仍然遇到问题。我决定全新开始。
这个简单的应用程序执行以下操作:
现在,我的问题在于:
这是我的MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, DataActivity.class);
intent.putExtra("First", true);
startActivity(intent);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter iff = new IntentFilter(MainActivity.class.getCanonicalName());
Log.d(getClass().getSimpleName(), "Creating Intent for Position Debuging...");
LocalBroadcastManager.getInstance(this).registerReceiver(AGDReceiver, iff);
}
@Override
protected void onDestroy() {
super.onDestroy();
LocalBroadcastManager.getInstance(this).unregisterReceiver(AGDReceiver);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private volatile ArrayAdapter<Integer> aradInt;
public BroadcastReceiver AGDReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final int broadcast = intent.getExtras().getInt("iInt");
Bundle b = intent.getExtras().getBundle("bBundle");
final int bI = b.getInt("bInt");
Log.d(getClass().getSimpleName(), "onReceieve(): " + broadcast);
initArrAd();
runOnUiThread(new Runnable() {
public void run() {
Log.d(getClass().getSimpleName(), "runOnUiThread(): " + broadcast);
aradInt.add(broadcast);
aradInt.setNotifyOnChange(true);
aradInt.notifyDataSetChanged();
}
});
}
};
private void initArrAd() {
if (aradInt == null) {
aradInt = new ArrayAdapter<Integer>(this.getApplicationContext(),R.id.listView1);
}
}
}
DataActivity.java
public class DataActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data);
}
private volatile int count = 0;
@Override
protected void onResume() {
super.onResume();
Log.d(getClass().getSimpleName(), "onResume(): " + count);
count++;
Intent intent = new Intent(MainActivity.class.getCanonicalName());
Bundle b = new Bundle();
b.putInt("bInt", count);
intent.putExtra("iInt", count);
intent.putExtra("bBundle", b);
this.moveTaskToBack(true);
for (int i = 1; i < 100; i++) {
LocalBroadcastManager.getInstance(this).sendBroadcastSync(intent);
intent.removeExtra("iInt");
intent.removeExtra("bInt");
intent.putExtra("bInt", count++);
intent.putExtra("iInt", count);
}
Log.d(getClass().getSimpleName(), "onResume(): Done");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.data, menu);
return true;
}
}
至于更新ListView,我想一旦我一直得到我想要的数据,并且MainActivity的视图是可见的,这应该是微不足道的。
以防万一,我的清单。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.eks.mytestbed"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.eks.mytestbed.MainActivity"
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="com.eks.mytestbed.DataActivity"
android:label="@string/title_activity_data" >
</activity>
</application>
</manifest>
更新:看起来问题的广播方面已关闭(我需要将注销调用放在.onDestroy()方法而不是.onPause()),但更新MainActivity上的列表视图不起作用!
答案 0 :(得分:1)
问题是您没有注册BroadcastReceiver
。您必须注册接收器才能使用它,因此您必须在完成后取消注册。
因为你已经使用了LocalBroadcastReceiver
注册
LocalBroadcastManager.getInstance(this).registerReceiver(
AGDReceiver ,
new IntentFilter(INTENT_FILTER_NAME));
取消注册
LocalBroadcastManager.getInstance(this).unregisterReceiver(
AGDReceiver );
另一件事是你的aradLong没有初始化