2016-12-28 4 views
-1

мой код для отправки sms запланирован не работает, смс не отправляется через 10 секунд. WHY?SmsManager и AllarmAlert не работает

я покажу вам мой код:

MainActivity:

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 
    public void scheduleAlarm(View V){ 
     //Long time= new GregorianCalendar().getTimeInMillis()+24*60*60*1000; 
     //Long time= new GregorianCalendar().getTimeInMillis()+10000; 
     //Long time= System.currentTimeMillis() + 10000; 

     Intent intentAlarm= new Intent(this, AlarmReceiver.class); 
     AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
     //alarmManager.set(AlarmManager.RTC_WAKEUP, time, PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT)); 
     alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT)); 

     Toast.makeText(this, "Alarm Scheduled", Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

BroadcastReceiver:

public class AlarmReceiver extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     String phoneNumberReciver="1234556"; 
     String message="blablabla"; 
     android.telephony.SmsManager sms= SmsManager.getDefault(); 
     sms.sendTextMessage(phoneNumberReciver, null, message, null, null); 
     Toast.makeText(context, "Alarm Triggered and SMS Sent", Toast.LENGTH_LONG); 

    } 

} 

Заранее спасибо!

+0

не отправить смс с brodcast приемника, но создать намерение службы –

ответ

0

использовать этот, чтобы установить будильник в 10 секунд!

alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000, 
          PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT)); 
+0

не работает так же .... – Markella92

+0

отредактирован! попробуйте сейчас –

+0

не работает .... – Markella92

0

попробовать это, а также положить отправить код смс намерению службы:

if (SDK_INT < Build.VERSION_CODES.KITKAT) { 
      alarmManager.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+10000, pendingIntent); 

     } 
     else if (Build.VERSION_CODES.KITKAT <= SDK_INT && SDK_INT < Build.VERSION_CODES.M) { 
      alarmManager.setExact(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+10000,pendingIntent); 

     } 
     else if (SDK_INT >= Build.VERSION_CODES.M) { 
      alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+10000,pendingIntent); 

     } 
+0

какая версия вы используете? –

+0

KITKAT .......... – Markella92

+0

ваш тост показывает или нет? –

Смежные вопросы