我有一个闹钟,想要选择用户选择音乐。 我有一些代码的活动,包括这个片段:
Button ring_button = (Button) findViewById(R.id.button2);
ring_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent_for_ring_button = new Intent();
intent_for_ring_button.setAction(Intent.ACTION_GET_CONTENT);
File file = new File(REPORTS_DIRECTORY);
intent_for_ring_button.setDataAndType(Uri.fromFile(file),"audio/*");
startActivityForResult(Intent.createChooser(intent_for_ring_button,"Open folder"), 0);
}
});
还有一个带有闹钟的活动,扩展 BroadcastReceiver ,我开始下一个活动,带有一些音乐的对话窗口。 这是它:
public class Dialog_window extends ActionBarActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_window_bckg);
getSupportActionBar().hide();
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
final MediaPlayer Alarm_mp3 = MediaPlayer.create(Dialog_window.this, R.raw.data_don_t_sing);
Alarm_mp3.start();
Alarm_mp3.setLooping(true);
AlertDialog.Builder builder = new AlertDialog.Builder(Dialog_window.this);
builder.setTitle("Важное сообщение!")
.setMessage("Вставай!")
.setIcon(R.drawable.uncle_sam)
.setCancelable(false)
.setNegativeButton("ОК, встаю, встаю.",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
Alarm_mp3.stop();
System.exit(0);
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
我怎么能意识到这一点?我必须从第一项活动中得到一些额外的东西或其他的东西吗?
答案 0 :(得分:0)
使用var word = "aabb"
let numberOfChars = word.characters.count // 4
let numberOfDistinctChars = Set(word.characters).count // 2
let occurrenciesOfA = word.characters.filter { $0 == "A" }.count // 0
let occurrenciesOfa = word.characters.filter { $0 == "a" }.count // 2
let occurrenciesOfACaseInsensitive = word.characters.filter { $0 == "A" || $0 == "a" }.count // 2
print(occurrenciesOfA)
print(occurrenciesOfa)
print(occurrenciesOfACaseInsensitive)
意图调用startActivityForResult
后,您应该覆盖同一类中的ACTION_GET_CONTENT
,并获取所选文件的路径。获得路径后,您可以保存到共享首选项,并轻松从其他活动中检索(例如,在onActivityResult
中)并使用Dialog_window
进行游戏。
MediaPlayer
在另一项活动中检索后,只需简单地播放即可。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 0){ // you've set to 0 at startActivityForResult
if(resultCode==RESULT_OK){
String filePath = data.getData().getPath();
// save filePath to shared preferences
}
}
}
将文件路径保存到共享首选项:
String savedPath = ..... ; // retrieve from shared preferences
//check your saved path, it could be a full path already..
//if it is, you don't need to concatenate anything to it
String fullPath = Environment.getExternalStorageDirectory()+savedPath;
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(fullPath);
mediaPlayer.prepare();
mediaPlayer.start()
从共享偏好设置恢复文件路径:
SharedPreferences.Editor editor = getSharedPreferences("mypref", MODE_PRIVATE).edit();
editor.putString("audioFilePath", filePath);
editor.commit();