0
MainActivity
에서 알람을 설정하고 다른 활동 (FakeRinger
활동)에서 알람을 중지하고 있습니다.다른 활동에서 알람을 중지하는 방법
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
클래스 : 이것은 MainActivity
코드
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;
}
}
코드에서 사용중인 코드는 작동하지 않습니다. 알람이 멈추지 않습니다. 아무도 내가 잘못하고있는 곳을 말해 줄 수 있습니까? 당신이 FakeRinger 활동에서 AlarmReciever다시 이동하는 이유
: com.example.mani.fakecall를, PID : 16049 java.lang.IllegalArgumentException가 : 서비스의 의도가 명시해야합니다 : 의도 {}' – mani
체크 아웃 내 업데이트 된 대답. 코드에서 stopService (intent)를 제거하십시오. – Black
여전히 작동하지 않습니다. – mani