2013-04-02 3 views
2

Android에서 1 분마다 토스트 메시지를 표시하는 서비스를 Android에서 구현하려고합니다. 나는 안드로이드 개발에 익숙하지 않고 이것을 돕는 AlarmManager에 대해 배웠다.Android - 토스트 메시지 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

'sleep (...)'은 해킹입니다. OP가 원하는 것을 할 수있는 더 좋은 방법이 있습니다. – Squonk

0

타이머 개체를 만들어보십시오. scheduleAtFixedRate(TimerTask)을 사용하여 토스트 메시지를 반복하십시오.

1

복사 토스트 호에 대한 아래의 3 라인

타이머 타이머 = 새로운 타이머();
TimerTask updateProfile = 새 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 분마다 토스트 메시지를 표시하는 Android에서 서비스를 구현할 수 있습니다. 감사합니다. 유용하다고 생각합니다. –