0

I am showing toast message after every 20 seconds from current time but if I going out the app it is not working. Here is my code:

public class Main extends Activity {
    final static private long ONE_SECOND = 1000;
    final static private long TWENTY_SECONDS = ONE_SECOND * 20;

    PendingIntent pi;
    BroadcastReceiver br;
    AlarmManager am;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        br = new BroadcastReceiver() {
            @Override
            public void onReceive(Context c, Intent i) {
                Toast.makeText(c, "Rise and Shine!", Toast.LENGTH_LONG).show();
                Log.i("Receive message in every five seconds", "message");
            }
        };
        registerReceiver(br, new IntentFilter("com.authorwjf.wakeywakey"));
        pi = PendingIntent.getBroadcast(this, 0, new Intent(
                "com.authorwjf.wakeywakey"), 0);
        am = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));

        am.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime(),
                TWENTY_SECONDS, pi);
    }

    @Override
    protected void onDestroy() {
        am.cancel(pi);
        unregisterReceiver(br);
        super.onDestroy();
    }
}

My question is if the app is not running but still it can show toast message? How can it possible in android?

4

3 回答 3

0

试试这个代码我希望它的工作......

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    final Handler h=new Handler();
    final  Runnable r=new Runnable() {

    public void run() {
       // TODO Auto-generated method stub
        Toast.makeText(getBaseContext(),"Example OF Practicle 8",Toast.LENGTH_SHORT).show();
    }
 };

 Timer t=new Timer();
 t.scheduleAtFixedRate(new TimerTask() {

     @Override
     public void run() {
        // TODO Auto-generated method stub
        h.post(r);
     }
 },2000, 5000);
于 2014-11-25T05:24:27.630 回答
0

您必须创建一个包含处理程序的类更新器

它将定期执行(您可以定义此周期),如下所示:

    import android.os.Handler;
    public class Updater {

    private Handler mHandler = new Handler();
    private Runnable mStatusChecker;
    final static private long TWENTY_SECONDS = 20000;
    private int UPDATE_INTERVAL = TWENTY_SECONDS;

    public Updater(final Runnable updater){
        mStatusChecker = new Runnable() {
            @Override
            public void run() {
                updater.run();
                mHandler.postDelayed(this, UPDATE_INTERVAL);
            }
        };
    }

    public Updater(Runnable updater, int interval){
        this(updater);
        UPDATE_INTERVAL = interval;
    }

    public void startUpdates(){
        mStatusChecker.run();
    }

    public void stopUpdates(){
        mHandler.removeCallbacks(mStatusChecker);
    }}

而不是创建服务“ServiceOn”:

    public class ServiceOn extends Service {

Updater updater = new Updater(new Runnable() {

    @Override 
    public void run() {

    // put your code here
    //  toast or what you want
    }});

    @Override
    public IBinder onBind(Intent arg0)
    {
        return null;
    }

    @Override
    public void onCreate()
    {       
        updater.startUpdates();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy()
    {
        updater.stopUpdates();
        super.onDestroy();
    }}

最后在您的活动中,您可以调用此服务:

context.startService(new Intent(context, ServiceOn.class));

即使应用程序停止运行,这也将每 20 秒运行一次

于 2013-09-01T17:31:38.603 回答
0

您可以使用 setTimeout

setTimeout( function(){ 
    toastr.clear(); // User to Clear the Toast Message Popup
}, 1000 ); // We can set our own time interval to disappear the popup

希望对你有帮助

于 2016-05-31T12:35:56.887 回答