您好我是Java和Android的新手,我正在尝试使用带有计时器的alarmmanager在按下startButton后每隔10秒显示一次吐司。 10秒后什么也没发生,我不知道为什么。
您能查看我的代码并帮助我识别和解决问题吗?
三江源。
变量 - journeycount = chronometer,pendingIntent mAlarmSender
主类 - SafeDrive3Activity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
journeycount = (Chronometer) findViewById(R.id.journeycount);
// Watch for button clicks.
startButton = (Button) findViewById(R.id.startButton);
startButton.setOnClickListener(mStartListener);
resetButton = (Button) findViewById(R.id.resetButton);
resetButton.setOnClickListener(mResetListener);
}
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
int stoppedMilliseconds = 0;
String chronoText = journeycount.getText().toString();
String array[] = chronoText.split(":");
if (array.length == 2) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000
+ Integer.parseInt(array[1]) * 1000;
} else if (array.length == 3) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 60 * 1000
+ Integer.parseInt(array[1]) * 60 * 1000
+ Integer.parseInt(array[2]) * 1000;
}
journeycount.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
journeycount.start();
startButton.setText("Driving..."); // changes button text value to 'Driving...'
startButton.setOnClickListener(mStartListener);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
// add 10 seconds to calendar object
cal.add(Calendar.SECOND, 10);
mAlarmSender = PendingIntent.getBroadcast(SafeDrive3Activity.this,
0, new Intent(SafeDrive3Activity.this, MyTimeReceiver.class), 0);
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), mAlarmSender);
}
};
View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
Button startButton;
startButton = (Button) findViewById(R.id.startButton);
startButton.setOnClickListener(mStartListener);
journeycount.setBase(SystemClock.elapsedRealtime());
startButton.setText("Start Driving");
journeycount.setText("00:00");
journeycount.stop();
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(mAlarmSender);
}
};
};
第二课:
public class MyTimeReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "HOUR NOTIFICATION", Toast.LENGTH_LONG).show();
}
}
的Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Safe"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".SafeDrive3Activity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<receiver android:name="MyTimeReceiver"></receiver>
</activity>
</application>
</manifest>
答案 0 :(得分:1)
我认为你应该在这种情况下考虑不同的方法。为什么不使用刚启动消息的Timer?
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
Toast.makeText(SafeDrive3Activity.this, "Whatever", Toast.LENGTH_LONG).show();
}
}, 0, 10000);
答案 1 :(得分:0)
使用下面的代码集警报管理器
am.set(AlarmManager.RTC_WAKEUP,
SystemClock.elapsedRealtime(),mAlarmSender);
答案 2 :(得分:0)
尝试使用以下代码:
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent serviceIntent = new Intent(context, MyService.class);
PendingIntent pi = PendingIntent.getService(context, 0, serviceIntent, 0);
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 10000, pi);
我没有使用BroadcastReviver。我每隔10秒就拨打一次服务。
新解决方案:
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent serviceIntent = new Intent(context, MyActivity.class);
PendingIntent pi = PendingIntent.getService(context, 0, serviceIntent, 0);
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 10000, pi);
MyActivity:
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
Toast.makeText(context, "HOUR NOTIFICATION", Toast.LENGTH_LONG).show();
}
}