我是android开发的新手,我正在做这个程序来接收消息onReceive
。我想更新ListView
中fragment
中的那些数据,并且更新应该在后台进行。广播接收器已在全球注册
@Override
public void onReceive(Context context, Intent intent) {
Hover.initialize(context);
/* IncomingSMSReceiver incomingSMSReceiver = new IncomingSMSReceiver();
incomingSMSReceiver.onReceive(context,intent);*/
String uuid = intent.getStringExtra("response_message");
Log.d(TAG, " " + uuid);
if (intent.hasExtra("transaction_extras")) {
HashMap t_extras = (HashMap) intent.getSerializableExtra("transaction_extras");
/*if (t_extras.containsKey("confirmCode")) {
String confirmationCode = t_extras.get("confirmCode").toString();
Log.d(TAG, " "+confirmationCode);
}*/
if (t_extras.containsKey("Tsh")) {
String balance = t_extras.get("Tsh").toString();
Log.d(TAG," "+balance);
Bundle bundle = new Bundle();
bundle.putString("id", uuid);
bundle.putString("message", balance);
FragmentHistory fragmentHistory = new FragmentHistory();
fragmentHistory.setArguments(bundle);
}
}
}
片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// return inflater.inflate(R.layout.fragment_history, container,false);
View view = inflater.inflate(R.layout.fragment_history, container, false);
/* mRecyclerView =(RecyclerView) view.findViewById(R.id.recyclerView_history);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setHasFixedSize(true);
configAdapter();*/
dataSaver.initializeDataSaver(getActivity());
// listScore = view.findViewById(R.id.list);
return view;
}
@Override
public void onActivityCreated( Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//dataSaver.setViewList("Hello", "Hola");
dataSaver.getDataList(this.getActivity());
/*try {
Bundle bundle = getActivity().getIntent().getExtras();
dataSaver.setViewList(bundle.getString("id"), bundle.getString("message"));
} catch (Exception e) {
e.printStackTrace();
}*/
}
@Override
public void onResume() {
super.onResume();
dataSaver.getDataList(this.getActivity());
}
@Override
public void onPause() {
super.onPause();
Bundle arguments = getArguments();
if (arguments != null) {
handleArguments(arguments);
}
Bundle extras = getActivity().getIntent().getExtras();
if (extras != null) {
handleExtras(extras);
}
}
@Override
public void onStop() {
super.onStop();
Bundle arguments = getArguments();
if (arguments != null) {
handleArguments(arguments);
}
Bundle extras = getActivity().getIntent().getExtras();
if (extras != null) {
handleExtras(extras);
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
Bundle arguments = getArguments();
if (arguments != null) {
handleArguments(arguments);
}
Bundle extras = getActivity().getIntent().getExtras();
if (extras != null) {
handleExtras(extras);
}
Toast.makeText(getActivity(), "Destroyed", Toast.LENGTH_LONG).show();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Bundle arguments = getArguments();
}
private void handleArguments(Bundle arguments) {
// TODO
try {
String id =arguments.getString("id");
String message = arguments.getString("message");
dataSaver.setViewList(id, message);
} catch (Exception e) {
e.printStackTrace();
}
}
ListView适配器
private ArrayList<TransDetails> scores;
TransDetails transDetails;
private MySharedPreference sharedPreference;
private ListView listScore;
private HashSet<String> scoreset;
private Gson gson;
public void initializeDataSaver(Activity activity){
transDetails = new TransDetails();
scores = new ArrayList<>();
gson = new Gson();
sharedPreference = new MySharedPreference(activity);
getHighScoreListFromSharedPreference();
}
/*public void setGson(Gson gson) {
this.gson = gson;
}*/
public Gson getGson() {
return gson;
}
/*public void setActivity(Activity activityf){
activityf = getActivity();
}
public Activity getActivity(){
return getActivity();
}*/
public void getHighScoreListFromSharedPreference() {
//retrieve data from shared preference
String jsonScore = sharedPreference.getHighScoreList();
Type type = new TypeToken<List<TransDetails>>(){}.getType();
scores = gson.fromJson(jsonScore, type);
if (scores == null) {
scores = new ArrayList<>();
}
}
public void saveScoreListToSharedpreference(ArrayList<TransDetails> scoresList) {
//convert ArrayList object to String by Gson
String jsonScore = gson.toJson(scoresList);
//save to shared preference
sharedPreference.saveHighScoreList(jsonScore);
}
public void upDateList(final String id, final String message){
transDetails.setId(id);
transDetails.setId(message);
scores.add(0, transDetails);
}
public void setViewList(String trans_id, String trans_amount){
if (trans_id != null) {
transDetails.setId(trans_id);
transDetails.setMessage(trans_amount);
scores.add(0,transDetails); //add to scores list
saveScoreListToSharedpreference(scores);
}
}
public void getDataList(Activity activity){
listScore = activity.findViewById(R.id.list);
if (scores.size() == 0) {
Toast.makeText(activity, "No data in sharedPreferences", Toast.LENGTH_SHORT).show();
} else {
getHighScoreListFromSharedPreference();
//get data
//set adapter for listview and visible it
ListViewAdapter adapter = new ListViewAdapter(activity, scores);
listScore.setAdapter(adapter);
}
}
答案 0 :(得分:1)
片段不能独立工作,必须处于活动状态。
所以第一步是创建一个Activity并将片段放入其中。 那么您必须以进入广播接收器的意图开始该活动。像这样的东西:
context.startActivity(new Intent(this,ListActivity.class));
另一个问题是,当收到新消息时,您的startActivity将再次启动并重复创建。为避免此问题,您必须以这种方式向您的Intent添加一个标志:
context.startActivity(new Intent(this,ListActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
此标志是为了避免重新创建活动并将现有的活动置于前面。
这样,您传递给的意图和数据将以onNewIntent()
的活动方式接收,您可以在那里处理数据。
因此,除了片段中的setArguments之外,您必须从bundle开始进行意图:
context.startActivity(new Intent(this,ListActivity.class).putExtras(bundle).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));