Android 使用SharedReference保存后在onResume上获取错误的变量值

Android 使用SharedReference保存后在onResume上获取错误的变量值,android,sharedpreferences,oncreate,onresume,Android,Sharedpreferences,Oncreate,Onresume,我正试图在onPause中保存布尔计数,并使用SharedReference在onResume中检索该计数。但是当我第一次启动应用程序时,我得到了false值count。 我不知道为什么。我搜索了我的问题,找不到匹配的答案 以下是我的一项活动的代码: public class ListView extends AppCompatActivity { private DatabaseReference mDatabase; private ArrayList<String&g

我正试图在
onPause
中保存布尔计数,并使用
SharedReference
onResume
中检索该计数。但是当我第一次启动应用程序时,我得到了
false
count。 我不知道为什么。我搜索了我的问题,找不到匹配的答案

以下是我的一项活动的代码:

public class ListView extends AppCompatActivity {
    private DatabaseReference mDatabase;
    private ArrayList<String> url= new ArrayList<>();
    private ArrayList<String> listitems = new ArrayList<>();
    private ArrayAdapter mAdapter;
    private android.widget.ListView listView;
    private AlertDialog.Builder builder;
    private int posi;
    private boolean count =true;
    BroadcastReceiver onComplete;
    SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);

        Log.d("count onCreate",String.valueOf(count));
        sharedPreferences = getSharedPreferences("Count",0);

        builder = new AlertDialog.Builder(ListView.this);
        final View coordinatorLayout = findViewById(android.R.id.content);

        Intent intent = getIntent();
        final String uri=intent.getStringExtra("uri");

        final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        mAdapter = new CustomAdapter(this,listitems);
        listView = (android.widget.ListView)findViewById(R.id.listview);
        mDatabase = FirebaseDatabase.getInstance().getReference().child(uri);

        mDatabase.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                String key = dataSnapshot.getKey();
                String value= dataSnapshot.getValue(String.class);
                listitems.add(0,key);
                url.add(0,value);

                listView.smoothScrollToPosition(0);
                mAdapter.notifyDataSetChanged();
                listView.setAdapter(mAdapter);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                        if ((cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED) ||
                                (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED)) {

                            builder.setMessage("Do You Want To Download "+listitems.get(position)+" "+uri+ " ?");
                            builder.setTitle(R.string.confirm);
                            builder.setIcon(R.drawable.pdf);
                            builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                    posi = position;
                                    Intent intent1 = new Intent(ListView.this, downloadService.class);
                                    intent1.putExtra("uri", url.get(position));
                                    startService(intent1);
                                    Snackbar sb = Snackbar.make(coordinatorLayout, "Downloading in Background, See Notification", Snackbar.LENGTH_SHORT);
                                    View sbView = sb.getView();
                                    sbView.setBackgroundColor(getResources().getColor(R.color.material_orange));
                                    sb.show(); }
                            });
                            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                            AlertDialog alert1 = builder.create();
                            alert1.show();
                        }
                        else {
                            final View coordinatorLayout = findViewById(android.R.id.content);
                            Snackbar sb = Snackbar.make(coordinatorLayout, "No Internet Connection..!", Snackbar.LENGTH_SHORT);
                            View sbView = sb.getView();
                            sbView.setBackgroundColor(getResources().getColor(R.color.material_orange));
                            sb.show();

                        }

                    }
                });
            }
            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            }
            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
            }
            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(getApplicationContext(),"Something Went Wrong :(" ,Toast.LENGTH_SHORT).show();
            }
        });
        onComplete = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                AlertDialog.Builder builder = new AlertDialog.Builder(ListView.this);
                builder.setTitle("Download Completed");
                builder.setMessage("Do You Want To Open "+listitems.get(posi)+" "+uri+ " ?");
                builder.setIcon(R.drawable.pdf);
                builder.setNegativeButton("Later", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setPositiveButton("Open", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        Intent inten = new Intent(ListView.this,onDownload.class);
                        inten.putExtra("uri",url.get(posi));
                        startService(inten);

                    }
                });
                AlertDialog alert = builder.create();
                alert.show();

            }
        };
    }
    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(onComplete);
        Log.d("count onPause",String.valueOf(count));
        sharedPreferences.edit().putBoolean("count",count).apply();

    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        count =sharedPreferences.getBoolean("count",true);
        Log.d("count onResume",String.valueOf(count));
        if (count){
            count=false;
            Toast.makeText(getApplicationContext(),"Loading...",Toast.LENGTH_SHORT).show();
        }

    }
}
公共类ListView扩展了AppCompative活动{
私有数据库参考数据库;
私有ArrayList url=新ArrayList();
private ArrayList listitems=new ArrayList();
私人ArrayaAdapter mAdapter;
私有android.widget.ListView ListView;
私有AlertDialog.Builder;
私人int posi;
私有布尔计数=真;
广播接收机未完成;
SharedReferences SharedReferences;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u list\u视图);
Log.d(“countoncreate”,String.valueOf(count));
SharedReferences=GetSharedReferences(“计数”,0);
builder=newalertdialog.builder(ListView.this);
最终视图coordinatorLayout=findViewById(android.R.id.content);
Intent=getIntent();
最终字符串uri=intent.getStringExtra(“uri”);
最终ConnectivityManager cm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_服务);
mAdapter=新的CustomAdapter(此为listitems);
listView=(android.widget.listView)findViewById(R.id.listView);
mDatabase=FirebaseDatabase.getInstance().getReference().child(uri);
mDatabase.addChildEventListener(新的ChildEventListener(){
@凌驾
公共void onChildaded(@NonNull DataSnapshot DataSnapshot,@null字符串s){
String key=dataSnapshot.getKey();
String value=dataSnapshot.getValue(String.class);
添加(0,键);
添加(0,值);
listView.smoothScrollToPosition(0);
mAdapter.notifyDataSetChanged();
setAdapter(mAdapter);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、最终整型位置、长id){
if((cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState()==NetworkInfo.State.CONNECTED)||
(cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState()==NetworkInfo.State.CONNECTED)){
setMessage(“是否要下载”+listitems.get(position)+“+uri+”?”;
builder.setTitle(R.string.confirm);
setIcon(R.drawable.pdf);
setPositiveButton(“确认”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
dialog.dismise();
posi=位置;
Intent intent1=新的Intent(ListView.this,downloadService.class);
intent1.putExtra(“uri”,url.get(position));
startService(intent1);
Snackbar sb=Snackbar.make(协调布局,“在后台下载,请参阅通知”,Snackbar.LENGTH_SHORT);
View sbView=sb.getView();
sbView.setBackgroundColor(getResources().getColor(R.color.material_orange));
某人展示();}
});
setNegativeButton(“否”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
dialog.dismise();
}
});
AlertDialog alert1=builder.create();
alert1.show();
}
否则{
最终视图coordinatorLayout=findViewById(android.R.id.content);
Snackbar sb=Snackbar.make(坐标布局,“无互联网连接…”,Snackbar.LENGTH\u SHORT);
View sbView=sb.getView();
sbView.setBackgroundColor(getResources().getColor(R.color.material_orange));
某人展示;
}
}
});
}
@凌驾
public void onChildChanged(@NonNull DataSnapshot DataSnapshot,@null字符串s){
}
@凌驾
公共void onChildRemoved(@NonNull DataSnapshot DataSnapshot){
}
@凌驾
public void onChildMoved(@NonNull DataSnapshot DataSnapshot,@null字符串s){
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError){
Toast.makeText(getApplicationContext(),“出现问题:(”,Toast.LENGTH_SHORT).show();
}
});
onComplete=新广播接收器(){
@凌驾
公共void onReceive(上下文、意图){
AlertDialog.Builder=新建AlertDialog.Builder(ListView.this);
builder.setTitle(“下载完成”);
setMessage(“是否要打开”+listitems.get(posi)+“+uri+”?”;
建筑商