我的计划是计算一些食物的卡路里。这是CS课程的一种做法。我是Android操作系统的初学者,我不知道为什么我的接收过程无法完成或显示在Activity上。
我只是在这里写相关的代码片段。我在sqlite数据库上有一些项目,我可以使用Log.d方法在数据库上检查我的项目,它们没问题。
App1的:
清单:
<receiver android:name="ItemBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.example.furkan.datagenerator" />
</intent-filter>
</receiver>
Received.class
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.received);
ItemBroadcastReceiver br = new ItemBroadcastReceiver();
Cursor cursor = (Cursor) br.receivedItem;
ReceivedItemsAdapter adapter = new ReceivedItemsAdapter(this, cursor);
ListView listView = getListView();
listView.setAdapter(adapter);
}
适配器:
public class ReceivedItemsAdapter extends CursorAdapter {
public ReceivedItemsAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.received, parent, false);
return retView;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
//ImageView icon = (ImageView) view.findViewById(R.id.icon);
TextView name = (TextView) view.findViewById(R.id.name);
TextView cal = (TextView) view.findViewById(R.id.amountCal);
TextView unit = (TextView) view.findViewById(R.id.unit);
unit.setText(" cal");
name.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
cal.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2))));
}}
广播接收器:
public class ItemBroadcastReceiver extends BroadcastReceiver {
Item receivedItem;
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
receivedItem = new Item(bundle.getString("name"),
Integer.parseInt(bundle.getString("calories")),
bundle.getString("category"),
null);
}}
App2的:
MainActivity:
public class MainActivity extends AppCompatActivity {
final int DELAY = 60000;
ReceivedItem item;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ReceivedItemsDatabaseHelper dbHelper = new ReceivedItemsDatabaseHelper(this);
item = new ReceivedItem(dbHelper.fetchReceivedItem().getString(
dbHelper.fetchReceivedItem().getColumnIndex(
dbHelper.fetchReceivedItem().getColumnName(1))),
Integer.parseInt(dbHelper.fetchReceivedItem().getString(
dbHelper.fetchReceivedItem().getColumnIndex(
dbHelper.fetchReceivedItem().getColumnName(2)))),
dbHelper.fetchReceivedItem().getString(
dbHelper.fetchReceivedItem().getColumnIndex(
dbHelper.fetchReceivedItem().getColumnName(3))), null);
Thread thread = new Thread() {
public void run() {
synchronized (this) {
try {
Log.d("Response ", item.getName()+", "+item.getCategory()+", "+item.getCalories());
Intent intent = new Intent();
intent.setAction("com.example.furkan.datagenerator");
intent.putExtra("name", item.getName());
intent.putExtra("calories", item.getCalories());
intent.putExtra("category", item.getCategory());
sendBroadcast(intent);
sleep(DELAY);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
thread.start();
}}
清单:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:0)
IntentFilter filter = new IntentFilter();
filter.addAction("com.example.furkan.datagenerator");
ItemBroadcastReceiver br = new ItemBroadcastReceiver();
registerReceiver(br, filter);
答案 1 :(得分:0)
您应该再次阅读BroadcastReceivers
。
ItemBroadcastReceiver br = new ItemBroadcastReceiver();
Cursor cursor = (Cursor) br.receivedItem;
ReceivedItemsAdapter adapter = new ReceivedItemsAdapter(this, cursor);
ListView listView = getListView();
listView.setAdapter(adapter);
这将创建接收器的新实例,并将receivedItem
分配给cursor
。但receivedItem
为null
,因为您从未为广播注册br
,所以永远不会null
。我也不确定,如果Item
可以投放到Cursor
。
考虑到您在此处发布的代码以及您使用的标题,使用BroadcastReceiver
可能根本不是一个好主意。您应该尝试通过意图将数据传递到Received
或直接从持久存储加载数据。
最后你的thread
没有任何意义。