如何停止来自其他活动的警报

时间:2017-12-18 20:36:23

标签: android alarmmanager

我正在设置来自df3的警报并将其从其他活动(MainActivity活动)中停止。 这是FakeRinger代码:

MainActivity

这是public class MainActivity extends AppCompatActivity { public static final String LOG_TAG = "MainActivity"; ImageView imageView; EditText number, name; Button setTimeButton; FloatingActionButton fab; Calendar calendar; long selectedTimeInMillis; TimePickerDialog timePicker; String enteredName, enteredNumber; Intent intent; PendingIntent pendingIntent; private static final int SELECT_IMAGE = 100; @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); fab = findViewById(R.id.floatingActionButton); 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) { 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; } if (minute < 10) { String minuteStr = "0" + String.valueOf(minute); minute = Integer.parseInt(minuteStr); Log.v(LOG_TAG, "minute is :" + minute); } Toast.makeText(MainActivity.this, hourOfDay + ":" + minute, Toast.LENGTH_SHORT).show(); Log.v(LOG_TAG, "Selected time in millis:" + selectedTimeInMillis); } }, hour, minute, false); timePicker.show(); } }); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { enteredName = name.getText().toString().trim(); enteredNumber = number.getText().toString().trim(); intent = new Intent(MainActivity.this, AlarmReciever.class); intent.putExtra("FAKE_NAME", enteredName); intent.putExtra("FAKE_NUMBER", enteredNumber); pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, 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(){ enteredName = name.getText().toString().trim(); enteredNumber = number.getText().toString().trim(); Calendar calendar = Calendar.getInstance(); calendar.get(Calendar.HOUR_OF_DAY); calendar.get(Calendar.MINUTE); long millis = calendar.getTimeInMillis(); intent = new Intent(MainActivity.this, AlarmReciever.class); intent.putExtra("FAKE_NAME", enteredName); intent.putExtra("FAKE_NUMBER", enteredNumber); pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); if (alarmManager != null) { alarmManager.set(AlarmManager.RTC_WAKEUP, millis+10000, 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(); } } } 类:

AlarmReceiver

这是我要停止闹钟的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); } } 活动。

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)); } if(getFakeName().isEmpty()){ displayName.setText(getResources().getString(R.string.unknown_name)); } else { displayName.setText(getFakeName()); } if(getFakeNumber().isEmpty()){ displayNumber.setText(getResources().getString(R.string.unknown_number)); } else { displayNumber.setText(getFakeNumber()); } 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(), 0, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); stopService(intent); if (alarmManager != null) { alarmManager.cancel(pendingIntent); } } }); } private String getFakeName(){ String contactName = null; Intent i = getIntent(); Bundle bundle = i.getExtras(); if (bundle != null) { contactName = bundle.getString("USER_FAKE_NAME"); } return contactName; } private String getFakeNumber(){ String contactNumber = null; Intent i = getIntent(); Bundle bundle = i.getExtras(); if (bundle != null) { contactNumber = bundle.getString("USER_FAKE_NUMBER"); } return contactNumber; } } 活动中使用的代码无效。警报没有停止。谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

您能说出为什么要从 FakeRinger 活动导航回 AlarmReciever 吗?

<强> FakeRinger

 Intent intent = new Intent(getApplicationContext(), AlarmReciever.class);
 PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);

尝试使用,

<强> FakeRinger

 Intent intent = new Intent();
 PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
 AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
 if (alarmManager != null) {
        alarmManager.cancel(pendingIntent);
 }

希望这有帮助。