如何取消其他活动的警报

时间:2017-12-20 18:14:57

标签: android alarmmanager

在我的应用中,我在MainActivity设置闹钟,我想从其他活动(FakeRinger)取消闹钟。这是我的代码。

MainActivity

public class MainActivity extends AppCompatActivity {

    public static final String LOG_TAG = "MainActivity";

    ImageView imageView;
    EditText number, name;
    Button setTimeButton;
    ImageButton imageButton;
    TimePickerDialog timePicker;
    long selectedTimeInMillis;
    String enteredName, enteredNumber;

    private static final int SELECT_IMAGE = 100;

    public static final int REQUEST_CODE_PI = 1001;

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

        PreferenceManager.setDefaultValues(this, R.xml.settings, false);

        imageView = findViewById(R.id.image_view);
        setTimeButton = findViewById(R.id.set_time);
        imageButton = findViewById(R.id.place_call_button);

        number = findViewById(R.id.number);
        name = findViewById(R.id.name);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent galleryIntent = new Intent(Intent.ACTION_PICK);
                galleryIntent.setType("image/*");
                startActivityForResult(galleryIntent, SELECT_IMAGE);
            }
        });

        setTimeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Calendar calendar = Calendar.getInstance();
                int hour = calendar.get(Calendar.HOUR_OF_DAY);
                int minute = calendar.get(Calendar.MINUTE);
                timePicker = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                        calendar.set(Calendar.MINUTE, minute);
                        calendar.set(Calendar.SECOND, 0);
                        calendar.set(Calendar.MILLISECOND, 0);
                        selectedTimeInMillis = calendar.getTimeInMillis();

                        if (hourOfDay > 12) {
                            hourOfDay = hourOfDay - 12;
                        }

                        Toast.makeText(MainActivity.this, hourOfDay + ":" + minute, Toast.LENGTH_SHORT).show();

                        Log.v(LOG_TAG, "Selected time in millis:" + selectedTimeInMillis);
                    }
                }, hour, minute, false);
                timePicker.show();
            }
        });

        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                enteredName = name.getText().toString().trim();
                enteredNumber = number.getText().toString().trim();
                if (enteredNumber.isEmpty()){
                    Toast.makeText(MainActivity.this, "Please enter  a number", Toast.LENGTH_LONG).show();
                    return;
                }

                Intent intent = new Intent(MainActivity.this, AlarmReciever.class);
                intent.putExtra("FAKE_NAME", enteredName);
                intent.putExtra("FAKE_NUMBER", enteredNumber);

                PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), REQUEST_CODE_PI, intent, 0);

                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                if (alarmManager != null) {
                    alarmManager.set(AlarmManager.RTC_WAKEUP, selectedTimeInMillis, pendingIntent);
                    Toast.makeText(MainActivity.this, "Your call has been placed", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "Error: something wrong", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.settings:
                Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
                startActivity(intent);
                return true;

            case R.id.quick_call:
                quickCall();
                return true;

                default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            try {
                Uri imageUri = data.getData();
                InputStream imageStream = null;
                if (imageUri != null) {
                    imageStream = getContentResolver().openInputStream(imageUri);
                }
                Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                imageView.setImageBitmap(selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show();
            }

        } else {
            Toast.makeText(this, "You haven't picked any Image", Toast.LENGTH_SHORT).show();
        }
    }

    public void quickCall(){

        Calendar calendar = Calendar.getInstance();
        calendar.get(Calendar.HOUR_OF_DAY);
        calendar.get(Calendar.MINUTE);
        long millis = calendar.getTimeInMillis();

        Intent quickCallIntent = new Intent(MainActivity.this, AlarmReciever.class);

        PendingIntent quickCallPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), REQUEST_CODE_PI, quickCallIntent, 0);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        if (alarmManager != null) {
            alarmManager.set(AlarmManager.RTC_WAKEUP, millis + 10000, quickCallPendingIntent);
            Toast.makeText(MainActivity.this, "Your call has been placed", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, "Error: something wrong", Toast.LENGTH_SHORT).show();
        }
    }
}

这是AlarmReciever类。

public class AlarmReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String getFakeName = intent.getStringExtra("FAKE_NAME");
        String getFakeNumber = intent.getStringExtra("FAKE_NUMBER");

        Intent fakeRinger = new Intent();
        fakeRinger.setClassName("com.example.mani.fakecall", "com.example.mani.fakecall.FakeRinger");
        fakeRinger.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        fakeRinger.putExtra("USER_FAKE_NAME", getFakeName);
        fakeRinger.putExtra("USER_FAKE_NUMBER", getFakeNumber);
        context.startActivity(fakeRinger);

        if(getFakeName != null){
            Log.v("Fake name is", getFakeName);
        }
        if(getFakeNumber != null){
            Log.v("Fake number is", getFakeNumber);
        }
    }
}

这是我要取消闹钟的FakeRinger活动。

public class FakeRinger extends AppCompatActivity {

    TextView displayName, displayNumber, displayCarrier;
    ImageButton alarmOffButton;
    String carrierName;
    ImageView displayImage;

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

        displayName = findViewById(R.id.display_name);
        displayNumber = findViewById(R.id.display_number);
        displayCarrier = findViewById(R.id.network_operator);
        displayImage = findViewById(R.id.display_image);

        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if (telephonyManager != null) {
            carrierName = telephonyManager.getNetworkOperatorName();
        }

        if (carrierName != null) {
            displayCarrier.setText(getResources().getString(R.string.incoming_call, carrierName));
        } else {
            displayCarrier.setText(getResources().getString(R.string.incoming_call));
        }

        Intent intent = getIntent();
        String userFakeNumber = intent.getExtras().getString("USER_FAKE_NUMBER");
        displayNumber.setText(userFakeNumber);

        displayName.setText(getFakeName());

        alarmOffButton = findViewById(R.id.hang_up_call);
        alarmOffButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), AlarmReciever.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1001, intent, 0);
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                if (alarmManager != null) {
                    alarmManager.cancel(pendingIntent);
                }
            }
        });
    }

    private String getFakeName(){
        String contacName = null;
        Intent intent = getIntent();
        contacName = intent.getExtras().getString("USER_FAKE_NAME");
        if (contacName.isEmpty()){
            contacName = "Unknown";
        }
        return contacName;
    }
}

我在FakeRinger活动中使用的代码无效。我不知道为什么。

1 个答案:

答案 0 :(得分:0)

快速而肮脏的方式是使用静态调用:

class MainActivity extends AppCompatActivity{
static MainActivity instance;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    instance = this;
}

public static MainActivity getInstance() {
    return instance;
}
....
 public void cancelAlarm(){
 if (alarmManager != null) {
                    alarmManager.cancel(pendingIntent);
                }
}

并在课堂上FakeAlarm:

class FakeAlarm extends AppCompatActivity{
  public void  clearData(){
      MainActivity a = MainActivity.getInstance();
      a.clear();
    }
}

漫长道路:

为什么不将MainActivityAppCompatActivity更改为FragmentActivity并创建FakeRinger  作为Fragment。这样,您可以更轻松地在父FragmentActivity和子Fragments之间共享组件和方法。有两个独立的父母" (活动)在尝试共享方法/数据时会产生更多问题/障碍。

例如,您可以在MainActivity(FragmentActivity)中使用此功能一次:

 AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

然后有一个方法取消MainActivity中的警报:

public void cancelAlarm(){
 if (alarmManager != null) {
                    alarmManager.cancel(pendingIntent);
                }
}

如果您愿意,请在另一个片段(FakeAlarm)中调用该方法:

((MainActivity)getActivity()).cancelAlarm();

Pass object data between fragments and activity in android

Passing Object from Fragment to Activity

Passing an Object from an Activity to a Fragment