onHandleIntent方法只在ServiceIntent类中运行一次[Android]

时间:2018-05-08 05:14:04

标签: android android-studio

我试图在android中的ServiceIntent类的onHandleIntent方法中每隔1秒调用一次api。

onHandleIntent只执行一次,我想每隔一秒运行一次这个方法..

FancyBookMakingService.class

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {

            if (intent != null) {
                try {
                    Thread.sleep(1000);

                    String result = getApi(URL); //API calling
                    Intent intentFBM = new Intent(DataHolder.ACTION_SEND_DATA);
                    intentFBM.putExtra("key", result);
                    sendBroadcast(intentFBM);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

    }

广播接收器

我有两个ServiceIntent Class,第一个正常工作另一个不是& BroadcastReceiverSignalr 是一个内部类

public class BroadcastReceiverSignalr extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (action.equalsIgnoreCase(DataHolder.ACTION_SEND_ACTIVE)) {
                String result = intent.getStringExtra(DataHolder.keySIGNALR);
                Log.i("TAG",result);

            }else if (action.equalsIgnoreCase(DataHolder.ACTION_SEND_DATA)){
                String data = intent.getStringExtra("key");
                Log.i("TAG",data);
            }
        }
    }

AndroidMainfest.xml

<service android:name=".FancyBookMakingService" />

1 个答案:

答案 0 :(得分:0)

使用Service类而不是IntentService并使用像

这样的计时器

公共类MainActivity扩展了AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startService(new Intent(this,Service.class)); //start service which is Service.java
}

}

,您的服务类看起来像是

public class Service extends Service {

public static final int notify = 300000;  //interval between two services(Here Service run every 5 Minute)
private Handler mHandler = new Handler();   //run on another Thread to avoid crash
private Timer mTimer = null;    //timer handling

@Override
public IBinder onBind(Intent intent) {
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
    if (mTimer != null) // Cancel if already existed
        mTimer.cancel();
    else
        mTimer = new Timer();   //recreate new
    mTimer.scheduleAtFixedRate(new TimeDisplay(), 0, notify);   //Schedule task
}

@Override
public void onDestroy() {
    super.onDestroy();
    mTimer.cancel();    //For Cancel Timer
    Toast.makeText(this, "Service is Destroyed", Toast.LENGTH_SHORT).show();
}

//class TimeDisplay for handling task
class TimeDisplay extends TimerTask {
    @Override
    public void run() {
        // run on another thread
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                // display toast
                Log.d("service is ","running");
            }
        });
    }
}