我开发了一个应用程序,用户可以为指定的任务设置铃声
这里是appointment.class的代码:
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri)
null);
this.startActivityForResult(intent, 5);
并且Activity扩展到BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()){
case AppointmentPage.RESULT_OK:
Log.e("Sample","asdas");
break;
}
我的问题是我在Logcat中看不到Sample。我怎么能看到它?
答案 0 :(得分:0)
您应该在活动中使用onActivityResults为用户保存他的偏好,然后在onRecieve中检索它以使用它
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 5: {
if (resultCode == RESULT_OK ) {
Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (uri != null) {
SharedPreferences sharedPreference = getSharedPreferences("key_for_shared_preference",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreference.edit();
editor.putString("selected_ringtone_uri",uri.toString());
editor.apply();
}
break;
}
}
}
}
为此,您需要从同一活动中激活意图
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri)
null);
startActivityForResult(intent, 5);
每次都在onReceive的broadcastReceiver中检索它
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPreference = getSharedPreferences("key_for_shared_preference",MODE_PRIVATE);
String ringtoneUri = sharedPreference.getString("selected_ringtone_uri","");
if(!ringtoneUri.contentEquals("")){
Log.d("ringtone uri",ringtoneUri);
}
}