完成进度条后调用方法

时间:2019-11-06 11:26:20

标签: java android

我需要制作一个以10为增量显示进度条之后发送SMS消息的应用程序。我已经设置了操作栏,我想应该使用AsyncTask吗?用户在主要活动中输入数字和消息,这就是我设置SendSMS方法的地方,不确定如何进行此工作,以便将其转发到AsyncTask并在那里调用该方法。

公共类MainActivity扩展了AppCompatActivity {

 <select class="custom-select" id="campaignDropdown" [(ngModel)]="campaignInfo" placeholder="Select Campaign" (change)="hitApiFunction(campaignInfo)">
            <option value="" disabled selected>Select Campaign</option>
              <option *ngFor="let campData of campaigns" [ngValue]="campData"> 
                  {{campData.campaign_name}}
              </option>
            </select>

}

ASYNCTASK

公共类MyAsyncTask扩展         AsyncTask {

private int MY_PERMISSIONS_REQUEST_SMS_RECEIVE = 10;
private static final String SMS_SENT = "SMS_SENT";
private static final String SMS_DELIVERED= "SMS_DELIVERED";
private EditText etPoruka;
private EditText etBroj;
private ProgressDialog progressDialog;
private Context context = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initWidgets();
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.RECEIVE_SMS},
            MY_PERMISSIONS_REQUEST_SMS_RECEIVE);
}

private void initWidgets() {
    etPoruka = findViewById(R.id.etPoruka);
    etBroj = findViewById(R.id.etBroj);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_send:
            MyAsyncTask<Void, Void, Void> updateTask = new MyAsyncTask<Void, Void, Void>(context);
            updateTask.execute();

            break;
        case R.id.action_sent:
            Toast.makeText(this, R.string.add_test, Toast.LENGTH_LONG).show();
             // BROADCAST RECEIVER ??
            break;
        case R.id.action_received:
            Toast.makeText(this, R.string.add_test, Toast.LENGTH_LONG).show();
            //BROADCAST RECEIVER ??
            //BOUND SERVICE
            break;
        case R.id.action_exit:
            final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Close app");
            builder.setMessage("Are you sure?");
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                finish();

            }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

           dialogInterface.dismiss();
    }
});

AlertDialog dialog = builder.create();
    dialog.show();
}
    return true;

}
public void sendSMS() {
    try{
        SmsManager smgr = SmsManager.getDefault();
        smgr.sendTextMessage(etBroj.getText().toString(),null,etPoruka.getText().toString(),null,null);
        Toast.makeText(MainActivity.this, "SMS sent successfully", Toast.LENGTH_SHORT).show();
    }
    catch (Exception e){
        Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
    }
}

1 个答案:

答案 0 :(得分:4)

您可以创建一个接口并在MainActivity中实现。

有关更多详细信息,请检查以下答案:

how do i send data back from onPostExecute in an AsyncTask?

How to pass data from an asynchronous task to an activity that called it?