How make Activity run to service

北慕城南 提交于 2020-01-15 10:59:06

问题


I'm new to java and would like to know how to do an activity run along with a service in the background. So that when the activity is closed and reopened it continues along with the service. I do not know how to explain it.

Suppose 3 services where each one is executed every hour.

Service 1... 1 hour ... Service 2 ... 1 hour ... Service 3. Finished.

And each time it is executed a textview is set visible in the activity. However when the activity is closed the textviews are not created.

The only way I found was by using variables as shown in the example below

Service 1:

public int service_one_done = 1;

Service 2:

public int service_two_done = 1;

Service 3:

public int service_three_done = 1;

Activity onCreate:

if (service_one_done == 1) { textview_example1.setVisibility(View.VISIBLE)
} if (service_two_done == 1) { textview_example2.setVisibility(View.VISIBLE)
} if (service_three_done == 1) { textview_example3.setVisibility(View.VISIBLE)
}

I would like to know if there is a better way to do this


回答1:


In my Service class I wrote this

private static void sendMessageToActivity(Location l, String msg) {
    Intent intent = new Intent("GPSLocationUpdates");
    // You can also include some extra data.
    intent.putExtra("Status", msg);
    Bundle b = new Bundle();
    b.putParcelable("Location", l);
    intent.putExtra("Location", b);
    LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}

and at the Activity side we have to receive this Broadcast message

LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
            mMessageReceiver, new IntentFilter("GPSLocationUpdates"));

By this way you can send message to an Activity. here mMessageReceiver is the class in that class you will perform what ever you want....

in my code I did this....

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Get extra data included in the Intent
        String message = intent.getStringExtra("Status");
        Bundle b = intent.getBundleExtra("Location");
        lastKnownLoc = (Location) b.getParcelable("Location");
        if (lastKnownLoc != null) {
            tvLatitude.setText(String.valueOf(lastKnownLoc.getLatitude()));
            tvLongitude
                    .setText(String.valueOf(lastKnownLoc.getLongitude()));
            tvAccuracy.setText(String.valueOf(lastKnownLoc.getAccuracy()));
            tvTimestamp.setText((new Date(lastKnownLoc.getTime())
                    .toString()));
            tvProvider.setText(lastKnownLoc.getProvider());
        }
        tvStatus.setText(message);
        // Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }
};

reference here



来源:https://stackoverflow.com/questions/44412727/how-make-activity-run-to-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!