我正在尝试从服务中获取数据,然后将其用于活动中。我已经how does a service return result to an activity和How to get data from service to activity进行了搜索,该解决方案无法正常工作。基本上,我的应用程序使用服务来显示时间,并且当用户关闭应用程序然后再次打开应用程序时,我想将服务中的时间提取到活动中以对该时间执行一些操作。这是下面的代码。我感谢有人能帮助我。谢谢
我的服务
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import com.alexeducationchina.alexeducation.FullTime_KINDERGARTEN.Timer_FullTime_K;
import com.alexeducationchina.alexeducation.R;
import java.util.Timer;
import java.util.TimerTask;
import static com.alexeducationchina.alexeducation.App.CHANNEL_ID;
public class ServiceTimer_K extends Service {
private int THE_ID_TO_UPDATE = 1;
private static Timer timer = new Timer();
private Context ctx;
private int second = 0 ;
NotificationManager notificationManager ;
private int minute = 0 ;
private int hour = 0 ;
//we are going to use a handler to be able to run in our TimerTask
final Handler handler = new Handler();
NotificationCompat.Builder notification ;
@Override
public void onCreate() {
super.onCreate();
super.onCreate();
ctx = this;
}
private class mainTask extends TimerTask
{
public void run()
{
second = second + 1 ;
if (second == 60){
minute++ ;
second = 0 ;
}
if (minute == 60){
hour++;
minute = 0 ;
second = 0 ;
}
notification.setContentTitle( hour + "h " + minute + "m " + second+"s");
notificationManager.notify(THE_ID_TO_UPDATE , notification.build());
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (timer != null){
timer.cancel();
}
timer = new Timer( );
//its nothing
/*
.putExtra("currentTime", hour + "h " + minute + "m " + second+"s");
LocalBroadcastManager.getInstance(ctx).sendBroadcast(notificationIntent);
*/
String input = intent.getStringExtra("inputExtra");
Intent notificationIntent = new Intent(this, Timer_FullTime_K.class)
.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0) ;
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification = new NotificationCompat.Builder(this, CHANNEL_ID);
notification.setContentText("Kindergarten");
notification.setSmallIcon(R.mipmap.logoback);
notification.setOnlyAlertOnce(true);
notification.setWhen(System.currentTimeMillis());
notification.setPriority(Notification.PRIORITY_DEFAULT);
notification.setContentIntent(pendingIntent);
notification.setLights(Color.RED, 1000, 1000);
notification.setVibrate(new long[]{0, 400, 250, 400});
notification.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
notification.build();
notificationManager.notify(THE_ID_TO_UPDATE , notification.build());
startForeground(THE_ID_TO_UPDATE, notification.build());
timer.scheduleAtFixedRate(new mainTask(), 0, 1000);
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
timer.cancel();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void sendBroadcast (boolean success){
Intent intent = new Intent ("timeIncoming"); //put the same message as in the filter you used in the activity when registering the receiver
intent.putExtra("currentTime", hour + "h " + minute + "m " + second+"s");
LocalBroadcastManager.getInstance(ctx).sendBroadcast(intent);
}
}
MyActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timer__full_time_k);
LocalBroadcastManager.getInstance(this).registerReceiver(
mMessageReceiver, new IntentFilter("timeIncoming"));
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("currentTime");
Toast.makeText(context, "Current Time Has Come : " + message, Toast.LENGTH_SHORT).show();
}
};
上述方法对我不起作用,因此我决定将数据放入通知的目的,因此当我单击显示时间的通知时,我会在活动中获取数据。但是给我空
此处是我如何定义通知的意图
Intent notificationIntent = new Intent(this, Timer_FullTime_K.class)
.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra("currentTime", hour + "h " + minute + "m " + second+"s");
LocalBroadcastManager.getInstance(ctx).sendBroadcast(notificationIntent);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0) ;
并将数据收集到活动中
//onCreate Method
try {
Intent intent = getIntent() ;
String message = intent.getStringExtra("currentTime");
Toast.makeText(Timer_FullTime_K.this, "Current Time Has Come : " + message, Toast.LENGTH_SHORT).show();
}catch(Exception c){
Toast.makeText(this, "No Intent Found", Toast.LENGTH_SHORT).show();
}