设置值后,calendar.getTimeInMillis()不起作用?

时间:2015-08-13 22:02:26

标签: android service calendar broadcastreceiver

我从我的服务中呼叫接收器,我需要在特定时间呼叫接收器。 我确实设法为服务和接收器编码!并且工作正常。

我调用calendar.getInstance()并在alarmManger中使用它“按原样”挂起意图时它可以正常工作

但是当我在日历“INSTANCE”中更改值时

calendar1.set(Calendar.HOUR_OF_DAY, 2);
calendar1.set(Calendar.MINUTE, 45);
calendar1.set(Calendar.SECOND, 0);

我的接收器永远不会被称为

这是我的完整代码

服务类:

package zafus.mysmsapp;

/**
 * Created by Tatla-4077 on 13/08/2015.
 */

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

import java.util.Calendar;


public class MySMSService extends Service
{

    private PendingIntent pendingIntent1;
    private PendingIntent pendingIntent2;
    private PendingIntent pendingIntent3;
    private PendingIntent pendingIntent4;
    private PendingIntent pendingIntent5;
    private String number="";
    private String text="";
    @Override
    public IBinder onBind(Intent arg0)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate()
    {
        // TODO Auto-generated method stub
        super.onCreate();
    }



    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        System.out.println("starting service");
        Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
        //openSurveyFromIntent(intent);

        number=intent.getStringExtra("number");
        text=intent.getStringExtra("text");
        Calendar calendar1 = Calendar.getInstance();

        //calendar1.setTimeInMillis(System.currentTimeMillis());

       // calendar1.set(Calendar.HOUR_OF_DAY, 2);
        //calendar1.set(Calendar.MINUTE, 45);
        //calendar1.set(Calendar.SECOND, 0);

        Intent myIntent = new Intent(); myIntent.setAction("com.zafus.sms");
        myIntent.putExtra("number", number);
        myIntent.putExtra("text1", "kill yourself");
        pendingIntent1 = PendingIntent.getBroadcast(this, 0, myIntent,PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC, calendar1.getTimeInMillis(), pendingIntent1);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent1);



        Calendar calendar2 = Calendar.getInstance();
        //calendar2.setTimeInMillis(System.currentTimeMillis());

        calendar2.set(Calendar.HOUR_OF_DAY, 12);
        calendar2.set(Calendar.MINUTE, 0);
        calendar2.set(Calendar.SECOND, 0);

        Intent myIntent1 = new Intent(); myIntent1.setAction("com.zafus.sms");
        myIntent1.putExtra("number", number);
        myIntent1.putExtra("text1", text);
        pendingIntent2 = PendingIntent.getBroadcast(this.getApplicationContext(), 0, myIntent1,PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager alarmManager1 = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager1.set(AlarmManager.RTC, calendar2.getTimeInMillis(), pendingIntent2);
        alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP, calendar2.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent2);




        Calendar calendar3 = Calendar.getInstance();
        //calendar3.setTimeInMillis(System.currentTimeMillis());

        calendar3.set(Calendar.HOUR_OF_DAY, 16);
        calendar3.set(Calendar.MINUTE, 0);
        calendar3.set(Calendar.SECOND, 0);

        Intent myIntent2 = new Intent(); myIntent2.setAction("com.zafus.sms");
        myIntent2.putExtra("number", number);
        myIntent2.putExtra("text1", text);
        pendingIntent3 = PendingIntent.getBroadcast(this.getApplicationContext(), 0, myIntent2,PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager alarmManager2 = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager2.set(AlarmManager.RTC, calendar3.getTimeInMillis(), pendingIntent3);
        alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP, calendar3.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent3);





        Calendar calendar4 = Calendar.getInstance();
        //calendar4.setTimeInMillis(System.currentTimeMillis());

        calendar4.set(Calendar.HOUR_OF_DAY, 20);
        calendar4.set(Calendar.MINUTE, 0);
        calendar4.set(Calendar.SECOND, 0);

        Intent myIntent3 = new Intent(); myIntent3.setAction("com.zafus.sms");
        myIntent3.putExtra("number", number);
        myIntent3.putExtra("text1", text);
        pendingIntent4 = PendingIntent.getBroadcast(this.getApplicationContext(), 0, myIntent3,PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager alarmManager3 = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager3.set(AlarmManager.RTC, calendar4.getTimeInMillis(), pendingIntent4);
        alarmManager3.setRepeating(AlarmManager.RTC_WAKEUP, calendar4.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent4);




        Calendar calendar5 = Calendar.getInstance();
        //calendar5.setTimeInMillis(System.currentTimeMillis());

        calendar5.set(Calendar.HOUR_OF_DAY, 22);
        calendar5.set(Calendar.MINUTE, 0);
        calendar5.set(Calendar.SECOND, 0);

        Intent myIntent4= new Intent(); myIntent4.setAction("com.zafus.sms");
        myIntent4.putExtra("number", number);
        myIntent4.putExtra("text1", text);
        pendingIntent5 = PendingIntent.getBroadcast(this.getApplicationContext(), 0, myIntent4,PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager alarmManager4 = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager4.set(AlarmManager.RTC, calendar5.getTimeInMillis(), pendingIntent5);
        alarmManager4.setRepeating(AlarmManager.RTC_WAKEUP, calendar5.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent5);





        return START_REDELIVER_INTENT;
    }


    @Override
    public void onDestroy()
    {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(),"Get Destroyed Service",Toast.LENGTH_LONG).show();
        // unregisterReceiver(receiver);
        super.onDestroy();
    }

} 

这是我的接收器类

package zafus.mysmsapp;

/**
 * Created by Tatla-4077 on 13/08/2015.
 */

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.widget.Toast;

public class MySMSReceiver extends BroadcastReceiver
{

    String number="";
    String text="";
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(context, "Get Called", Toast.LENGTH_LONG).show();

        number=intent.getStringExtra("number");
        text=intent.getStringExtra("text1");
        if(number.length()>0){
            sendSMS(number,text);
        }


    }

    private void sendSMS(String phoneNumber, String message)
    {
        SmsManager sms = SmsManager.getDefault();
        try {
            sms.sendTextMessage(phoneNumber, null, message, null, null);
        }
        catch (Exception e){

        }
    }
}

这似乎真的很愚蠢,但我在这个问题上浪费了几个小时。请帮帮我!

0 个答案:

没有答案