Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中及时听取变化_Android_Android Intent - Fatal编程技术网

如何在Android中及时听取变化

如何在Android中及时听取变化,android,android-intent,Android,Android Intent,如果电话时间改变了,我需要做一些动作。我如何倾听时间的变化 更具体地说,我想在用户输入的特定日期到来时发送短信 我通过服务运行它。关键是我需要经常检查发送短信的日期是否已经到了 import java.text.SimpleDateFormat; import java.util.Date; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import an

如果电话时间改变了,我需要做一些动作。我如何倾听时间的变化

更具体地说,我想在用户输入的特定日期到来时发送短信

我通过服务运行它。关键是我需要经常检查发送短信的日期是否已经到了

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.gsm.SmsManager;

public class SmsService extends Service{
    String date, messege, numberphone;
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        date=intent.getStringExtra("date"); 
        String[] dateArray = date.split(",");
        Date dateTime = new Date();


        if(dateArray[0].equals(Integer.toString(dateTime.getDate(
                ))) &&dateArray[1].equals(Integer.toString(dateTime.getMonth(
                ))) && dateArray[2].equals(Integer.toString(dateTime.getYear())))   
            sendSMS(intent.getStringExtra("numberphone"),intent.getStringExtra("message"));
        return super.onStartCommand(intent, flags, startId);
    }



    @SuppressWarnings("deprecation")
    private void sendSMS(String phoneNumber, String message)
    {        
        PendingIntent pi = PendingIntent.getActivity(this, 0,
                new Intent(this, SmsActivity.class), 0);

        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, pi, null);        
    }  


}

使用action
“android.intent.action.TIME\u SET”
创建一个BroadCastReceiver并设置Receiver类

在AndroidManifest文件中

 <receiver android:name=".MyReceiver">
            <intent-filter >
                <action android:name="android.intent.action.TIME_SET"/>
            </intent-filter>
 </receiver>
getActivity().registerReceiver(networkReceiver,新的IntentFilter(“android.intent.action.TIME_SET”)

但问题是,在更改日期时,接收者已经被销毁,因为我正在活动暂停时注销接收者

public class MyReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(final Context arg0, Intent arg1) {
        Log.d("MyReceiver", "Time Changed");
    }
}