我使用AlarmManager每隔一段时间调用一次活动,但是,必须更改间隔。该活动只是我已更改为pendingIntent的另一个函数。 调用类如下所示,也是主类。被调用的类在调用类之后显示。最后,是清单文件,它可能包含错误源。
Timer_test:
package com.timer_test;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Timer_testActivity extends Activity {
/** Called when the activity is first created. */
private PendingIntent mAlarmSender;
int update_freq = 1;
Intent toast_make;
int interval;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.button1);
Button button3 = (Button)findViewById(R.id.button3);
Button button5 = (Button)findViewById(R.id.button5);
Button button10 = (Button)findViewById(R.id.button10);
toast_make = new Intent("com.timer_test.toast_testClass");
mAlarmSender = PendingIntent.getService(Timer_testActivity.this, 0, toast_make ,0);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
update_freq = 1;
timer_schedule();
}
});
button3.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
update_freq = 3;
timer_schedule();
}
});
button5.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
update_freq = 5;
timer_schedule();
}
});
button10.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
update_freq = 10;
timer_schedule();
}
});
}
public void timer_schedule(){
long firstTime = SystemClock.elapsedRealtime();
interval = update_freq*1000;
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, interval, mAlarmSender);
}
package com.timer_test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class toast_test extends Activity{
public void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.main);
onClick("yes");
}
public void onClick(String n) {
Toast.makeText(toast_test.this,n,
Toast.LENGTH_SHORT).show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.timer_test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Timer_testActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.timer_test.toast_test"/>
</application>
</manifest>
答案 0 :(得分:1)
toast_test
是活动,在清单中您将其声明为服务