2013-04-02 73 views
2

我試圖在Android中實現一項服務,在Android中每1分鐘顯示一次Toast消息。我是Android開發的新手,並瞭解到AlarmManager會幫助我做到這一點。我已經實現了代碼的方式如下:Android - Toast消息每1分鐘

這是我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(); 
    } 
} 

你能告訴我什麼是錯的,需要做什麼才能糾正它?

非常感謝您提前。

+1

只是好奇...爲什麼你要這麼做? – Squonk

回答

-2

你可以創建一個包含你的代碼的循環線程。

像這樣:

public class Toaster extends Thread{      
    public void run(){    
    //Your code to loop 

    thread.sleep(60000)   
     } 
    } 

希望它能幫助!

+0

'睡眠(...)'是一個黑客。有更好的方法去做OP想要的。 – Squonk

0

嘗試創建一個Timer對象。然後使用scheduleAtFixedRate(TimerTask)來重複Toast消息。

1

複製低於3線吐司呼叫

定時器定時器=新定時器();
TimerTask updateProfile = new SampleService(SampleService.this); timer.scheduleAtFixedRate(updateProfile,10,1000);

class CustomTimerTask extends TimerTask { 
private Context context; 
private Handler mHandler = new Handler(); 
     // Write Custom Constructor to pass Context 
    public CustomTimerTask(Context con) { 
     this.context = con; 
    } 

    @Override 
    public void run() { 
    new Thread(new Runnable() { 
     @Override 
     public void run() { 
      mHandler.post(new Runnable() { 
      @Override 
      public void run() { 
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)", Toast.LENGTH_LONG).show();  
     } 
    }); 
} 
}).start(); 

} 

    } 
0
下面的代碼

嘗試,

MainActivity.java

public class MyService extends Service { 

public static final long INTERVAL=60000;//variable for execute services every 1 minute 
private Handler mHandler=new Handler(); // run on another Thread to avoid crash 
private Timer mTimer=null; // timer handling 

@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    throw new UnsupportedOperationException("unsupported Operation"); 
} 
@Override 
public void onCreate() { 
    // cancel if service is already existed 
     if(mTimer!=null) 
      mTimer.cancel(); 
    else 
      mTimer=new Timer(); // recreate new timer 
    mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(),0,INTERVAL);// schedule task 
} 
@Override 
public void onDestroy() { 
    Toast.makeText(this, "In Destroy", Toast.LENGTH_SHORT).show();//display toast when method called 
    mTimer.cancel();//cancel the timer 
} 
//inner class of TimeDisplayTimerTask 
private class TimeDisplayTimerTask extends TimerTask { 
    @Override 
    public void run() { 
     // run on another thread 
     mHandler.post(new Runnable() { 
      @Override 
      public void run() { 
       // display toast at every 1 minute 
       Toast.makeText(getApplicationContext(), "Notify", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 
} 
} 

MainActivity.java

public class MainActivity extends AppCompatActivity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main);//load the layout file 
    startService(new Intent(this,MyService.class));//use to start the services 
} 
} 

另外,在您的清單文件

AndroidManifest.xml中添加以下代碼

<service android:name=".MyService" 
     android:enabled="true"/> 
+0

這段代碼應該做什麼?代碼是不夠的。告訴你改變了什麼。將它壓縮到重要的部分。謝謝。 – kwoxer

+0

使用此代碼,您可以在Android中實現一項服務,每隔1分鐘顯示一次敬酒信息,謝謝,希望對您有所幫助。 –