电话状态空闲时增加浮点数

时间:2018-02-02 08:58:25

标签: java android sharedpreferences

一旦电话状态为IDLE,我试图增加一个浮点数。然后将浮点数保存在共享首选项中,并在主活动中公开它。

目前我所做的是使用意图接收电话状态的号码并发送到主要活动和我保存的主要活动。但是一旦我测试它,手机就会崩溃。以下是来电:

的当前代码
public class IncomingCall extends BroadcastReceiver {
    private View view;

    @Override
    public void onReceive(Context context, Intent intent) {

        try {
            System.out.println("Receiver start");
            Intent intentBundle = new Intent();
            Bundle bundle = new Bundle();
            bundle.putFloat("value", 0.5f);

            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                bundle.putFloat("value", 0.5f);
                intentBundle.putExtras(bundle);
                Toast toast = new Toast(context);
                CharSequence text = "O.5 Added";
                int duration = Toast.LENGTH_SHORT;
                Toast testtoast = Toast.makeText(context, text, duration);
                testtoast.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

以下是我在主要活动中的代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private float minteger = 0.5f;
    private Context context;
    private Activity activity;
    private static final int PERMISSION_REQUEST_CODE = 1;
    private View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        SharedPreferences sharedPreferences = this.getSharedPreferences("com.appname.app", Context.MODE_PRIVATE);
        sharedPreferences.edit().putFloat("value", minteger).apply();

        Float value = sharedPreferences.getFloat("value", 0.0f);

        TextView textv = (TextView) findViewById(R.id.numberValue);
        textv.setText(String.valueOf(value));

        context = getApplicationContext();
        activity = this;
        Button check_permission = (Button) findViewById(R.id.check_permission);
        Button request_permission = (Button) findViewById(R.id.request_permission);
        check_permission.setOnClickListener(this);
        request_permission.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        view = v;

        int id = v.getId();
        switch (id) {
            case R.id.check_permission:
                if (checkPermission()) {

                    Snackbar.make(view, "Permission already granted.", Snackbar.LENGTH_LONG).show();

                } else {

                    Snackbar.make(view, "Please request permission.", Snackbar.LENGTH_LONG).show();
                }
                break;
            case R.id.request_permission:
                if (!checkPermission()) {

                    requestPermission();

                } else {

                    Snackbar.make(view, "Permission already granted.", Snackbar.LENGTH_LONG).show();

                }
                break;
        }
    }

    private boolean checkPermission() {
        int result = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE);
        if (result == PackageManager.PERMISSION_GRANTED) {

            return true;

        } else {

            return false;

        }
    }

    private void requestPermission() {

        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.READ_PHONE_STATE)) {

            Toast.makeText(context, "Phone state allows you to earn on incoming calls. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();

        } else {

            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_PHONE_STATE}, PERMISSION_REQUEST_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    Snackbar.make(view, "Permission Granted, Now you can access phone manager.", Snackbar.LENGTH_LONG).show();

                } else {

                    Snackbar.make(view, "Permission Denied, You cannot access phone manager.", Snackbar.LENGTH_LONG).show();

                }
                break;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以将值放入IncomingCall中的sharedpref并在MainActivity中检索。