2017-11-02 3 views
0

내가 개발중인 앱을 사용하면 특정 시간 간격으로 기기가 자동 및 일반 모드를 활성화 할 수있는 시간을 설정할 수 있습니다. 지금까지는 시작 및 종료 시간에 대한 사용자 입력을 얻을 수 있지만 그 시간에 자동 모드로 장치를 가져올 수는 없습니다. 자동 및 일반 모드를 활성화하려면 보류중인 의도 및 브로드 캐스트 수신기를 사용하고 있습니다. 여기 내가 지금까지 코딩 한 내용이 있습니다.지연 의도가 트리거되지 않음

MainActivity.java

public class MainActivity extends AppCompatActivity { 

EditText etMeal,etDesert,start,end; 
Button btnAdd,btnView,btnsttime,btnend; 
DatabaseHelper myDB; 
int mHour,mMinute; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    btnAdd = (Button) findViewById(R.id.btnAdd); 
    btnView = (Button) findViewById(R.id.btnView); 
    btnsttime=(Button)findViewById(R.id.sttime); 
    btnend=(Button)findViewById(R.id.endtime); 

    etMeal = (EditText) findViewById(R.id.etMeal); 
    //etDesert = (EditText) findViewById(R.id.etDesert); 
    start=(EditText)findViewById(R.id.stime); 
    end=(EditText)findViewById(R.id.etime); 
    myDB = new DatabaseHelper(this); 

    btnAdd.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String meal = etMeal.getText().toString(); 
      //String desert = etDesert.getText().toString(); 

      if(meal.length() != 0){ 
       AddData(meal); 
       etMeal.setText(""); 
       //etDesert.setText(""); 
      }else{ 
       Toast.makeText(MainActivity.this, "You must fill in the text fields!",Toast.LENGTH_LONG).show(); 
      } 
     } 
    }); 

    btnView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(MainActivity.this, View_Foods.class); 
      startActivity(intent); 
     } 
    }); 

} 

public void AddData(String meal) { 

    boolean insertData = myDB.addData(meal); 

    if (insertData == true) { 
     Toast.makeText(MainActivity.this, "Data Successfully Inserted!", Toast.LENGTH_LONG).show(); 
    } else { 
     Toast.makeText(MainActivity.this, "Something went wrong :(.", Toast.LENGTH_LONG).show(); 
    } 
} 

public void start(View view) 
{ 
    // Get Current Time 
    /* Calendar c = Calendar.getInstance(); 
    mHour = c.get(Calendar.HOUR_OF_DAY); 
    mMinute = c.get(Calendar.MINUTE);*/ 

    Calendar calendar=Calendar.getInstance(); 

    // Launch Time Picker Dialog 
    TimePickerDialog timePickerDialog = new TimePickerDialog(this, 
      new TimePickerDialog.OnTimeSetListener() { 

       @Override 
       public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 

       Calendar newTime=Calendar.getInstance(); 

        newTime.set(Calendar.HOUR_OF_DAY, hourOfDay); 
        newTime.set(Calendar.MINUTE, minute); 
        String Gottime=String.valueOf(hourOfDay)+":"+String.valueOf(minute); 
        start.setText(Gottime); 

        //mHour=newobj.get(hourOfDay); 
        //mMinute=newobj.get(minute); 

        AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(ALARM_SERVICE); 

        //create a pending intent to be called at midnight 
        Toast.makeText(MainActivity.this, "Pending Intent started", Toast.LENGTH_LONG).show(); 
        PendingIntent midnightPI = PendingIntent.getService(MainActivity.this, 0, new Intent("applications.recyclerview1.SilenceBroadCastReceiver"), PendingIntent.FLAG_UPDATE_CURRENT); 

        //schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day 

        am.setRepeating(AlarmManager.RTC_WAKEUP, newTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, midnightPI); 

       } 
      },calendar.get((Calendar.HOUR_OF_DAY)),calendar.get((Calendar.MINUTE)), true); 
    timePickerDialog.show(); 
} 

public void end(View view) 
{ 
    // Get Current Time 
    Calendar c = Calendar.getInstance(); 
    mHour = c.get(Calendar.HOUR_OF_DAY); 
    mMinute = c.get(Calendar.MINUTE); 

    final Calendar newobj2=Calendar.getInstance(); 

    // Launch Time Picker Dialog 
    TimePickerDialog timePickerDialog = new TimePickerDialog(this, 
      new TimePickerDialog.OnTimeSetListener() { 

       @Override 
       public void onTimeSet(TimePicker view, int hourOfDay, 
             int minute) { 

        end.setText(hourOfDay + ":" + minute); 
        newobj2.get(hourOfDay); 
        newobj2.get(minute); 

        //create a pending intent to be called at 6 AM 
        AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(ALARM_SERVICE); 

        PendingIntent sixPI = PendingIntent.getService(MainActivity.this, 0, new Intent("applications.recyclerview1.UnsilenceBroadcastReceiver"), PendingIntent.FLAG_UPDATE_CURRENT); 

        //schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day 

        am.setRepeating(AlarmManager.RTC_WAKEUP, newobj2.getTimeInMillis(), AlarmManager.INTERVAL_DAY, sixPI); 

       } 
      }, mHour, mMinute, false); 
    timePickerDialog.show(); 
} 

SilentBroadCastReceiver.java

public class SilenceBroadCastReceiver extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 
    AudioManager audio=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE); 
    audio.setRingerMode(AudioManager.RINGER_MODE_SILENT); 
}} 

UnsilentBroadcastReceiver.java

public class UnsilenceBroadcastReceiver extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 
    AudioManager audio=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE); 
    audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL); 
}} 

의 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="applications.recyclerview1"> 

     <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".View_Foods"></activity> 
     <receiver android:name=".SilenceBroadCastReceiver"> 

      <intent-filter> 

       <action android:name="applications.recyclerview1.SilenceBroadCastReceiver" > 

       </action> 

      </intent-filter> 

     </receiver> 

      <receiver android:name=".UnsilenceBroadcastReceiver"> 

      <intent-filter> 



     <action 
     android:name="applications.recyclerview1.UnsilenceBroadcastReceiver" > 

       </action> 

      </intent-filter> 

     </receiver> 
     </application> 

     </manifest> 
,
+0

android 버전을 테스트하고 계십니까? –

+0

@SaranSankaran on Android 버전 : 5.1.1 –

답변

0

pendingItnent에서 일부 메시지를 브로드 캐스트하려면 PendingIntent.getBroadcast을 사용해야합니다. getService을 사용 중입니다.

+0

흠 .... 확인해 보겠습니다. –

+0

감사합니다. –