从活动到服务获取数据

时间:2017-04-16 06:16:51

标签: android android-activity service sharedpreferences

我有一个活动,它从日期选择器中取出日期并将其设置为编辑文本,并保存共享首选项。现在我想在服务类中使用这个日期。

以下代码显示了按钮点击后如何在共享首选项中保存数据。

 mSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
                SharedPreferences.Editor editor = pref.edit();

                editor.putString("Name", String.valueOf(mPersonName.getText()));
                editor.putString("Type", String.valueOf(mType.getText()));
                editor.putString("Date", mDate.getText().toString());
                editor.putString("UserDay", String.valueOf(day));
                editor.putString("UserMonth", String.valueOf(month+1));
                editor.putString("UserYear", String.valueOf(year));

                editor.commit();

                mPersonName.getText().clear();
                mType.getText().clear();
                mDate.getText().clear();

                newDate=pref.getString("Date","");

                Toast.makeText(getApplicationContext(),"userDate"+newDate,Toast.LENGTH_SHORT).show();

           }
        });
    }

这是我的服务类。

public class serviceToCheckTheDate extends Service {
    //@Nullable

    CustomerInformation mCstmr=new CustomerInformation();

    String currentDateTimeString = DateFormat.getDateInstance().format(new Date());

    Calendar mCalendar=Calendar.getInstance();
    int currentMonth=mCalendar.get(Calendar.MONTH)+1;
    int currentDay=mCalendar.get(Calendar.DAY_OF_MONTH);
    int currentYear=mCalendar.get(Calendar.YEAR);


   // SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
    //final String myDay=pref.getString("UserDay","");
   // final String myDate= pref.getString("Date","");


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


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        final Handler mHandler=new Handler();
        final int timeInterel=10000;
        final String UserDate=intent.getStringExtra("enteredDate");
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(currentYear==mCstmr.userYear)
                {
                    if(currentMonth==mCstmr.userMonth)
                    {
                     if((mCstmr.userDay-currentDay)<=5){
                         Toast.makeText(getApplicationContext(),"Namsthe",Toast.LENGTH_SHORT).show();
                     }
                    }
                }
                Toast.makeText(getApplicationContext(),"hellow"+UserDate,Toast.LENGTH_SHORT).show();
                Log.d("ser on","ser on");
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext());
                mBuilder.setSmallIcon(R.drawable.ic_launcher);
                mBuilder.setContentTitle("Document Management!");
                mBuilder.setContentText("Hi,One of your document is going to expire");
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                // notificationID allows you to update the notification later on.
                mNotificationManager.notify(0, mBuilder.build());
                mHandler.postDelayed(this,timeInterel);
            }
        },timeInterel);
        return START_STICKY;
    }
}

如何让newDate服务。

谢谢。

3 个答案:

答案 0 :(得分:0)

可以使用双向

1)使用意图

  

从活动传递意图数据并获取意图数据      服务中的onStartCommand()方法

2)您可以使用EventBus

定义事件:

div#down>a {
    margin: auto;
    display: block;
}

准备订阅者:声明并注释您的订阅方法,可选择指定线程模式:

select * from sys.schema_unused_indexes;

注册并取消注册您的订阅者。例如,在Android上,活动和片段通常应根据其生命周期进行注册:

public static class MessageEvent { /* Additional fields if needed */ }

发布活动:

@Subscribe(threadMode = ThreadMode.MAIN)  
public void onMessageEvent(MessageEvent event) {/* Do something */};

更多细节转到this link

答案 1 :(得分:0)

  

在活动中

   Intent intent = new Intent(this,Myservice.class);

   intent.putExtra("Name", String.valueOf(mPersonName.getText()));
   intent.putExtra("Type", String.valueOf(mType.getText()));
   intent.putExtra("Date", mDate.getText().toString());
   intent.putExtra("UserDay", String.valueOf(day));
   intent.putExtra("UserMonth", String.valueOf(month+1));
   intent.putExtra("UserYear", String.valueOf(year));

   startService(intent);
  

在服务中

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    String Name=intent.getStringExtra("Name");
    String Type=intent.getStringExtra("Type");
    String Date=intent.getStringExtra("Date");
    String UserDay=intent.getStringExtra("UserDay");
    String UserMonth=intent.getStringExtra("UserMonth");
    String UserYear=intent.getStringExtra("UserYear"); 

    return super.onStartCommand(intent, flags, startId);
}

答案 2 :(得分:0)

您可以使用intent传递数据。

活动:

  Intent intent = new Intent(this,Myservice.class);
  intent.putExtra("Name", mPersonName.getText().toString());
  intent.putExtra("Type", mType.getText().toString());
  intent.putExtra("Date", mDate.getText().toString());
  intent.putExtra("UserDay", String.valueOf(day));
  intent.putExtra("UserMonth", String.valueOf(month+1));
  intent.putExtra("UserYear", String.valueOf(year));
  startService(intent);

在服务中:

您可以通过从活动传递的意图获取onStartCommand()中的数据。

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    String Name=intent.getStringExtra("Name");
    String Type=intent.getStringExtra("Type");
    String Date=intent.getStringExtra("Date");
    String UserDay=intent.getStringExtra("UserDay");
    String UserMonth=intent.getStringExtra("UserMonth");
    String UserYear=intent.getStringExtra("UserYear"); 
    return super.onStartCommand(intent, flags, startId);
}