0

サービスを含むアプリケーションを作成しています。このサービスは、地理座標の取得など、長時間実行されるバックグラウンド アクティビティを実行する必要があります。ここで、このサービスが特定の時間間隔の後に、アラームのように何かが行われることをアプリに通知するようにします。私はこの部分で混乱しています。取得できません。サービスがいくつかのアクションについてアプリを更新し、アラート アラームなどを表示する方法を教えてください。

以下は私のコードです、

アクティビティ:

public class BroadcastTest extends Activity {
      private static final String TAG = "BroadcastTest";
      private Intent intent;

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

    intent = new Intent(this, BroadcastService.class);
}

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        updateUI(intent);
    }
};

@Override
public void onResume() {
    super.onResume();
    startService(intent);
    registerReceiver(broadcastReceiver, new IntentFilter(
            BroadcastService.BROADCAST_ACTION));
}

@Override
public void onPause() {
    super.onPause();
    unregisterReceiver(broadcastReceiver);
    stopService(intent);
}

private void updateUI(Intent intent) {
    boolean gps = intent.getBooleanExtra("gps", false);
    if (gps) {
        String counter = intent.getStringExtra("counter");
        String time = intent.getStringExtra("time");
        // Log.d(TAG, counter);
        // Log.d(TAG, time);

        TextView txtDateTime = (TextView) findViewById(R.id.txtDateTime);
        TextView txtCounter = (TextView) findViewById(R.id.txtCounter);
        txtDateTime.setText(time);
        txtCounter.setText(counter);
    } else {


    }
}

}

サービス:

 public class BroadcastService extends Service {
  private static final String TAG = "BroadcastService";
  public static final String BROADCAST_ACTION = "";
  private final Handler handler = new Handler();
  Intent intent;
  int counter = 0;

GPSTracker gps;

@Override
public void onCreate() {
    super.onCreate();

    intent = new Intent(BROADCAST_ACTION);

    //Notification notification = new Notification(R.drawable.icon, "MyService", 1000);
    //Intent notifIntent = new Intent(this, BroadcastTest.class);
    //PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifIntent, 0);
    //notification.setLatestEventInfo(getApplicationContext(), "ForeGroundService", "Service is Running", pendingIntent);
    //startForeground(1, notification);
}

@Override
public void onStart(Intent intent, int startId) {
    handler.removeCallbacks(sendUpdatesToUI);
    handler.postDelayed(sendUpdatesToUI, 1000); // 1 second

}

private Runnable sendUpdatesToUI = new Runnable() {
    public void run() {
        DisplayLoggingInfo();
        handler.postDelayed(this, 10000); // 10 seconds
    }
};

private void DisplayLoggingInfo() {
    gps = new GPSTracker(getApplicationContext());
    double latitude = 0, longitude = 0;

    // check if GPS enabled
    System.out.println(gps.isGPSEnabled); 
     if (gps.canGetLocation()) {
        latitude = gps.getLatitude();
        longitude = gps.getLongitude();

    } else {
        // can't get location
        // GPS or Network is not enabled
        // Ask user to enable GPS/network in settings
        //gps.showSettingsAlert();

    }


    Log.d(TAG, "entered DisplayLoggingInfo");
    intent.putExtra("time", Double.toString(latitude)); 
    intent.putExtra("counter", Double.toString(longitude)); 
    sendBroadcast(intent);
}

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

@Override
public void onDestroy() {
    handler.removeCallbacks(sendUpdatesToUI);
    super.onDestroy();
}
 }

通知を表示するために、4時間後または50km後など、特定の時間間隔後にアプリケーションをトリガーする方法を教えてください。

4

1 に答える 1