将微调器值传递给广播接收器

时间:2016-09-29 07:49:50

标签: android broadcastreceiver spinner

我正在创建一个从mySQL库中检索电话号码的应用程序,用这些值填充微调器,并且在计时器上调用旋转器列表中的数字。我的问题是如何将该微调器电话号码解析为调用电话的活动?我将删除不必要的代码部分。

public class MainActivity extends AppCompatActivity implements Spinner.OnItemSelectedListener{

    private Spinner spinner;
    private ArrayList<String> brojevi;
    private JSONArray result;
    private TextView textViewPoziv,textViewSms;

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

        //Inicijalizacija liste brojeva
        brojevi = new ArrayList<String>();

        //Inicijalizacija spinnera
        spinner = (Spinner) findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(this);

        textViewPoziv = (TextView) findViewById(R.id.textViewPoziv);
        textViewSms = (TextView) findViewById(R.id.textViewSms);

        //pozivanje URL-a
        getData();
}


    public void startAlert(View view) {
        EditText text = (EditText) findViewById(R.id.time);
        int i = Integer.parseInt(text.getText().toString());
        Intent intent = new Intent(this, SampleAlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), pendingIntent);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String text = spinner.getSelectedItem().toString();
        textViewPoziv.setText(getPoziv(position));
        textViewSms.setText(getSms(position));
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        textViewPoziv.setText("");
        textViewSms.setText("");
    }
}

我应该将该微调器值传递给:

public class SampleAlarmReceiver extends WakefulBroadcastReceiver {

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


        PackageManager pm = context.getPackageManager();
        int hasPerm = pm.checkPermission(Manifest.permission.CALL_PHONE,context.getPackageName());
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE)== PackageManager.PERMISSION_GRANTED){
            Intent myIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "+" + **SpinnerValue**));;
            myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(myIntent);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

pass spinner value in intent so that you will retrieve in onReceive() method  





    public class SampleAlarmReceiver extends WakefulBroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String spinnerValue="";
            if(intent!=null && intent.getStringExtra("key")!=null){
                 spinnerValue=intent.getStringExtra("key");
            }

            PackageManager pm = context.getPackageManager();
            int hasPerm = pm.checkPermission(Manifest.permission.CALL_PHONE,context.getPackageName());
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE)== PackageManager.PERMISSION_GRANTED){
                Intent myIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "+" + **SpinnerValue**));;
                myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(myIntent);
            }
        }
    }





public void startAlert(View view) {
        EditText text = (EditText) findViewById(R.id.time);
        int i = Integer.parseInt(text.getText().toString());
        Intent intent = new Intent(this, SampleAlarmReceiver.class);
        intent.putExtra("key", "Spinner Value");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), pendingIntent);
    }



I hope it will work for you