美好的一天,即时通过闹钟管理器创建一个关于提醒/待办事项列表的项目。 我有4个类(AlarmManager,AlarmReceiver,MainActivity,NoteEditorActivity)
MainActivity - 列出我的所有提醒
NoteEditorActivity - 我将在其中创建提醒
现在我正在向noteeditor类添加一个警报功能。
我的问题是如何将AlarmManagerActivity类调用到NoteEditor。
我的AlarmManagerActivity和AlarmReceiver类已经编码。我只是无法正常使用它。
AlarmManagerClass
package com.jayceejash.schedmeup;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AlarmManagerActivity extends Activity{
private Button mStartBtn;
private EditText mTxtSeconds;
private Toast mToast;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStartBtn = (Button) findViewById(R.id.btnSetAlarm);
mTxtSeconds = (EditText) findViewById(R.id.txtSeconds);
mStartBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
try
{
int i = Integer.parseInt(mTxtSeconds.getText().toString());
Intent intent = new Intent(AlarmManagerActivity.this,AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getActivity(AlarmManagerActivity.this,2, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(i * 1000), pendingIntent);
if (mToast != null)
{
mToast.cancel();
}
mToast = Toast.makeText(getApplicationContext(),"Alarm for activity is set in: " + i + "seconds", Toast.LENGTH_LONG);
mToast.show();
}catch(NumberFormatException e){
if (mToast != null)
{
mToast.cancel();
}
mToast = Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_LONG);
mToast.show();
Log.i("AlarmManagerActivity","NumberFormatException");
}
}
});
}
}
然后是我的AlarmReceiver
package com.jayceejash.schedmeup;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import java.io.IOException;
public class AlarmReceiver extends Activity{
private MediaPlayer mPlayer;
private PowerManager.WakeLock mWakeLock;
Button stopalarm;
@SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PowerManager pm=(PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock =pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "my wakelock");
mWakeLock.acquire();
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON );
setContentView(R.layout.activity_stop_alarm);
//Stop the alarm music
stopalarm=(Button) findViewById(R.id.btnStopAlarm);
stopalarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mPlayer.stop();
finish();
return;
}
});
PlaySound(this,getAlarmUri());
}
private void PlaySound(Context context,Uri alert){
mPlayer=new MediaPlayer();
try{
mPlayer.setDataSource(context,alert);
final AudioManager am=(AudioManager) getSystemService(Context.AUDIO_SERVICE);
if(am.getStreamVolume(AudioManager.STREAM_ALARM)!=0);
{
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mPlayer.prepare();
mPlayer.start();
}
}
catch(IOException e){
Log.i("AlaramReciever", "no audio file");
}
}
//Get an alarm sound. If none set, try notification, Otherwise, ringtone.
private Uri getAlarmUri()
{
Uri alert= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if(alert==null)
{
alert= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if(alert==null)
{
alert=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
return alert;
}
protected void onStop(){
super.onStop();
mWakeLock.release();
}
}
这是我的NoteEditorActivity Class
package com.jayceejash.schedmeup;
import com.jayceejash.schedmeup.data.NoteItem;
import com.jayceejash.schedmeup.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
public class NoteEditorActivity extends Activity {
private NoteItem note;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_note_editor);
}
else {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_note_editor_pre3);
}
Intent intent = this.getIntent();
note = new NoteItem();
note.setKey(intent.getStringExtra(NoteItem.KEY));
note.setText(intent.getStringExtra(NoteItem.TEXT));
EditText et = (EditText) findViewById(R.id.noteText);
et.setText(note.getText());
et.setSelection(note.getText().length());
}
public void saveAndFinish(View v) {
EditText et = (EditText) findViewById(R.id.noteText);
String noteText = et.getText().toString();
Intent intent = new Intent();
intent.putExtra(NoteItem.KEY, note.getKey());
intent.putExtra(NoteItem.TEXT, noteText);
setResult(RESULT_OK, intent);
finish();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
saveAndFinish(null);
}
return false;
}
@Override
public void onBackPressed() {
saveAndFinish(null);
}
}
如果你们有一个正在开发的教程或同一个项目,请帮助我,我只是新的android和我喜欢在这里开发。非常感谢
答案 0 :(得分:1)
如果您要从另一个活动中调用一个活动,则应使用Intents。 Intent提供Bundle对象,您可以通过它们传递状态信息。
来自
的更多信息http://developer.android.com/reference/android/content/Intent.html
An intent is an abstract description of an operation to be performed.
It can be used with startActivity to launch an Activity, broadcastIntent to
send it to any interested BroadcastReceiver components, and
startService(Intent) or bindService(Intent, ServiceConnection, int) to
communicate with a background Service.
An Intent provides a facility for performing late runtime binding between the
code in different applications. Its most significant use is in the launching of
activities, where it can be thought of as the glue between activities. It is
basically a passive data structure holding an abstract description of an action
to be performed.