我正在尝试在 Android 中实现一项服务,该服务在 Android 中每 1 分钟显示一条 toast 消息。我是 Android 开发的新手,并且了解了有助于我做到这一点的 AlarmManager。我已经通过以下方式实现了代码:
这是我的 IIManagerActivity 类
package com.example.iimanager;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Menu;
import android.widget.Toast;
public class IIManagerActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_iimanager);
AlarmManager mgr=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(this, SampleService.class);
PendingIntent pi=PendingIntent.getService(this, 0, i, 0);
mgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), AlarmManager.INTERVAL_FIFTEEN_MINUTES/900, pi);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_iimanager, menu);
return true;
}
}
这是我的 SampleService,用于显示敬酒消息。出于某种原因,无论我等待多长时间,我都无法看到敬酒消息。
package com.example.iimanager;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class SampleService extends IntentService {
public SampleService() {
super("SimpleService");
//Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)", Toast.LENGTH_LONG).show();
}
@Override
protected void onHandleIntent(Intent intent) {
//do something
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)", Toast.LENGTH_LONG).show();
}
}
你能告诉我哪里出了问题以及需要做些什么来纠正它吗?
非常感谢您提前。