未触发的意图未触发

时间:2017-11-02 06:23:41

标签: android broadcastreceiver android-pendingintent

我正在开发的应用程序将允许用户设置设备在特定时间间隔内激活静音和正常模式的时间。直到现在我可以获得用户输入的开始和结束时间,但是在那段时间内无法使设备成为静音模式。要激活静音和普通模式,我正在使用待定意图和广播接收器。这是我到目前为止编码的内容。

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>

1 个答案:

答案 0 :(得分:0)

如果要从pendingItnent广播某些消息,则应使用PendingIntent.getBroadcast。您正在使用getService