发送短信和报警管理器

时间:2015-04-22 03:33:56

标签: android alarmmanager android-alarms

在我的应用程序中,用户可以设置警报。一些自己的警报和一些他想要发送短信的人。但我的问题是警报在一些电话中不能正常工作,而且如果它的工作好,它不发送短信 这是我的AlarmService类:

 @Override
public void onStart(Intent intent, int startId) {

    setAlarm();

    super.onStart(intent, startId);
}

private void setAlarm(){
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Cursor cursor = G.drugsDatabase.rawQuery("SELECT * FROM drugs", null);
    long startDate;
    String number;
    while (cursor.moveToNext()) {
        int id = cursor.getInt(cursor.getColumnIndex("drug_id"));
        startDate = cursor.getLong(cursor.getColumnIndex("start_date"));
        String patientName = cursor.getString(cursor.getColumnIndex("patient_name"));
        number = cursor.getString(cursor.getColumnIndex("drug_receiver"));
        String drugNumber = cursor.getString(cursor.getColumnIndex("drug_count"));
        String unit = cursor.getString(cursor.getColumnIndex("drug_unit"));
        String drugType = cursor.getString(cursor.getColumnIndex("drug_type"));
        String drugName = cursor.getString(cursor.getColumnIndex("drug_name"));

        Intent myIntent = new Intent(G.context, ShowAlarmActivity.class);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        myIntent.putExtra("PATIENT_NAME", patientName);
        myIntent.putExtra("NUMBER",number);
        myIntent.putExtra("DRUG_NUMBER",drugNumber);
        myIntent.putExtra("UNIT",unit);
        myIntent.putExtra("TYPE",drugType);
        myIntent.putExtra("NAME",drugName);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, id, myIntent, PendingIntent.FLAG_ONE_SHOT);
        if (System.currentTimeMillis() - 5000 > startDate) {
            alarmManager.cancel(pendingIntent);
            continue;
        }
        alarmManager.set(AlarmManager.RTC_WAKEUP, startDate, pendingIntent);
    }
    cursor.close();
}

这是我的ShowAlarmActivity类:

   Bundle bundle = getIntent().getExtras();
    String number = "";
    String patientName = "";
    String drugCount = "";
    String drugType = "";
    String unit = "";
    String drugName = "";

    if (bundle !=null ){
        patientName=bundle.getString("PATIENT_NAME");
        number = bundle.getString("NUMBER");
        drugCount = bundle.getString("DRUG_NUMBER");
        drugName = bundle.getString("NAME");
        drugType = bundle.getString("TYPE");
        unit = bundle.getString("UNIT");
    }

    Log.i("LOG", "Number is : " + number);
    if (number !=null && number.length() > 5){
        sendMessage(number,"یادآوری می شود"+patientName+" "+drugCount+" "+" "+unit+" از "+drugType+" " + drugName+ " الان مصرف نماید. \n خداوند شفابخش و مهربان است.");
        ShowAlarmActivity.this.finish();
    }
}

private void sendMessage(String destination,String message){
    SmsManager smsManager = SmsManager.getDefault();
    int length = message.length();
    if(length > 160) {
        ArrayList<String> messageList = smsManager.divideMessage(message);
        smsManager.sendMultipartTextMessage(destination, null, messageList, null, null);
    }
    else {
        smsManager.sendTextMessage(destination, null, message, null, null);
    }
    Toast.makeText(G.context,"پیام با موفقیت ارسال شد",Toast.LENGTH_SHORT).show();
    Log.i("LOG","Message Sent");
}

我应该注意在我的国家电话号码是这样的:0919837xxxx

0 个答案:

没有答案