我在Voice Reminder上尝试我的项目。它基于录音并将其设置为闹钟。当闹钟被激活时,我的问题出现了。它被激活了两次。 这是我的代码。如果有人可以提供帮助,请随时欢迎。
AudioAppActivity (MainActivity file)
public class AudioAppActivity extends Activity {
private static MediaRecorder mediaRecorder;
private static MediaPlayer mediaPlayer;
private Button recordButton;
private Button playButton;
private Button stopButton;
private Button database;
private String audioFilePath;
private static int c = 0;
private boolean isRecording = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_app);
// INITIALISATION
recordButton = (Button) findViewById(R.id.button2);
playButton = (Button) findViewById(R.id.button1);
stopButton = (Button) findViewById(R.id.button3);
database = (Button) findViewById(R.id.button4);
// FETCHING OF STATIC VALUE OF COUNTER
SharedPreferences spf = getSharedPreferences("COUNTER_VALUE",
MODE_PRIVATE);
c = spf.getInt("counter", 0);
// CHECKING OF MICROPHONE
if (!hasMicrophone()) {
stopButton.setEnabled(false);
playButton.setEnabled(false);
recordButton.setEnabled(false);
} else {
playButton.setEnabled(false);
stopButton.setEnabled(false);
recordButton.setEnabled(true);
}
// ON CLICK OF RECORD BUTTON
recordButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// CREATING DIRECTORY FOR RECORDINGS
File f = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Mydir");
if (!f.exists()) {
f.mkdir();
}
audioFilePath = f.getAbsolutePath() + "/myaudio" + c + ".3gp";
c = c + 1;
isRecording = true;
playButton.setEnabled(false);
recordButton.setEnabled(false);
stopButton.setEnabled(true);
try {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder
.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setOutputFile(audioFilePath);
mediaRecorder
.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.prepare();
} catch (Exception e) {
e.printStackTrace();
}
mediaRecorder.start();
}
});
// ON CLICK OF STOP BUTTON
stopButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
stopButton.setEnabled(false);
playButton.setEnabled(true);
if (isRecording) {
recordButton.setEnabled(true);
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
isRecording = false;
} else {
mediaPlayer.release();
mediaPlayer = null;
recordButton.setEnabled(true);
}
}
});
// ON CLICK OF PLAY BUTTON
playButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
playButton.setEnabled(false);
recordButton.setEnabled(false);
stopButton.setEnabled(true);
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(audioFilePath);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
database.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(AudioAppActivity.this,
DatabaseActivity.class);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
protected boolean hasMicrophone() {
PackageManager pmanager = this.getPackageManager();
return pmanager.hasSystemFeature(PackageManager.FEATURE_MICROPHONE);
}
@Override
protected void onDestroy() {
SharedPreferences sp = this.getSharedPreferences("COUNTER_VALUE",
MODE_PRIVATE);
SharedPreferences.Editor spEditor = sp.edit();
spEditor.putInt("counter", c);
spEditor.commit();
super.onDestroy();
}
DatabaseActivity.java
public class DatabaseActivity extends Activity {
private static MediaPlayer mediaPlayer;
ListView lv;
int i = 0, pos;
String fileName;
List<String> filenames = new ArrayList<String>();
TimePicker tp;
TimePickerDialog tpd;
final static int RQS_1 = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_database);
lv = (ListView) findViewById(R.id.listView1);
File f2 = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Mydir");
if (!f2.exists()) {
f2.mkdir();
}
File[] files = f2.listFiles();
if (files.length > 0) {
for (File ff : files) {
filenames.add(ff.getName().toString());
}
lv.setAdapter(new ArrayAdapter<String>(DatabaseActivity.this,
android.R.layout.simple_list_item_1, filenames));
} else {
Toast.makeText(DatabaseActivity.this,
"There are no files...(onCreate)", Toast.LENGTH_LONG)
.show();
}
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
pos = position;
fileName = lv.getItemAtPosition(pos).toString();
File f = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Mydir");
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(f.getAbsolutePath() + "/" + fileName);
mediaPlayer.prepare();
mediaPlayer.start();
Toast.makeText(DatabaseActivity.this, "PLAYING...",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
});
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
fileName = lv.getItemAtPosition(pos).toString();
registerForContextMenu(lv);
return false;
}
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId() == R.id.listView1) {
menu.setHeaderTitle("File Options");
menu.add(0, 1, 0, "SCHEDULE");
menu.add(0, 2, 0, "DELETE");
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
int id = item.getItemId();
String v = lv.getItemAtPosition(pos).toString();
File f = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Mydir");
if (id == 1) {
openTimePickerDailog();
}
else if (id == 2) {
File file = new File(f.getAbsolutePath() + "/" + v);
boolean deleted = file.delete();
if (deleted == true) {
Toast.makeText(DatabaseActivity.this, "FILE DELETED!!!",
Toast.LENGTH_LONG).show();
Refresh();
} else {
Toast.makeText(DatabaseActivity.this, "FILE NOT DELETED!!!",
Toast.LENGTH_LONG).show();
}
}
return false;
}
public void openTimePickerDailog() {
Calendar cal = Calendar.getInstance();
tpd = new TimePickerDialog(DatabaseActivity.this, settime,
cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), false);
tpd.setTitle("Set Alarm Time");
tpd.show();
}
OnTimeSetListener settime = new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
calSet.set(Calendar.MINUTE, minute);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
if (calSet.compareTo(calNow) <= 0) {
// Today Set time passed, count to tomorrow
calSet.add(Calendar.DATE, 1);
}
setAlarm(calSet);
}
};
protected void setAlarm(Calendar calSet) {
Toast.makeText(
DatabaseActivity.this,
"Alarm Received for file:' " + fileName + "' at time "
+ calSet.getTime(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
intent.putExtra("key", fileName);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(),
pendingIntent);
sendBroadcast(intent);
}
protected void Refresh() {
lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(null);
File f2 = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Mydir");
if (!f2.exists()) {
f2.mkdir();
}
File[] files = f2.listFiles();
if (files.length > 0) {
for (File ff : files) {
filenames.add(ff.getName().toString());
}
lv.setAdapter(new ArrayAdapter<String>(DatabaseActivity.this,
android.R.layout.simple_list_item_1, filenames));
} else {
Toast.makeText(DatabaseActivity.this,
"There are no files...(onResume)", Toast.LENGTH_LONG)
.show();
}
super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.database, menu);
return true;
}
警报接收器
public class AlarmReceiver extends BroadcastReceiver {
private String filename;
private MediaPlayer mediaPlayer;
@Override
public void onReceive(Context context, Intent intent) {
filename = intent.getExtras().getString("key");
File f = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Mydir");
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(f.getAbsolutePath() + "/" + filename);
mediaPlayer.prepare();
mediaPlayer.start();
Toast.makeText(context, "PLAYING...",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
在创建待定意图时尝试使用PendingIntent.FLAG_ONE_SHOT
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getBaseContext(), RQS_1, intent, PendingIntent.FLAG_ONE_SHOT);
答案 1 :(得分:0)
我遇到了同样的问题而且有点奇怪但是我通过将我的alarmmanager初始化程序从onCreate方法移动到onStart方法来解决它,同时我删除了动态设置我的视图方向的代码行,现在从我的清单中设置它。 xml