手机重启后如何记住待处理的意图和警报?

时间:2014-07-25 18:14:38

标签: android broadcastreceiver android-pendingintent android-alarms android-broadcast

我对Android很新,我一直在制作一个应用程序,根据我的大学时间表将手机变成振动模式。我希望我的应用程序在重新启动手机后记住待处理的意图,这样每次重启设备时都不必提供给他们的时间表。

这是我的代码:

import java.util.ArrayList;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent; 
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;


public class Labs extends Activity implements OnClickListener {

private ArrayList <Calendar> Cal= new ArrayList <Calendar>(60);

private ArrayList <CheckBox> l= new ArrayList <CheckBox>(60);

private ArrayList <Boolean> checkboxValue_lab = new ArrayList <Boolean>(60);

private SharedPreferences mSharedPreferences_l;
private SharedPreferences.Editor mPrefsEditor_l;
private Button submitButton;



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


    submitButton = (Button)findViewById(R.id.button1);
    submitButton.setOnClickListener(this);

    mSharedPreferences_l = getSharedPreferences("loginPrefs", MODE_PRIVATE);
    mPrefsEditor_l = mSharedPreferences_l.edit();




    //Initialise check boxes for every slot
    for (int i = 101; i <= 160; i++) {
           String viewId = "checkBox" + i;
           l.add(i-101, (CheckBox) findViewById(getResources().getIdentifier(viewId, "id", getPackageName())));
           checkboxValue_lab.add(i-101, mSharedPreferences_l.getBoolean("lab"+(i-101), false));

           if (checkboxValue_lab.get(i-101) == true) {
               l.get(i-101).setChecked(true);}
           }
        }   



@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;
}   

public void addenable(){

}

public int Day(int a)
{
    int d=0;
    int x = a;
    x--;

    if(x>=30)
        d = (x/6)-3;

    else
        d=(x/6)+2;

    return d;
}

public int Hour(int a)
{
    int hr=0;
    int x=a;

    x--;

    hr=(x%6)+8;

    if((x%6)>=4)
        hr--;

    if(x>=30)
        hr = hr+6;

    return hr;

}

public int Min(int a)
{
    int m=0;
    int x=a;

    if(x%6==5)
        m=50;

    if(x%6==0)
        m=40;

    return m;
}

public void getdata(View V){

    for( int i=0; i<60; i++)
    {
        Cal.add(i, Calendar.getInstance());
        Cal.get(i).set(Calendar.DAY_OF_WEEK, Day(i+1));
        Cal.get(i).set(Calendar.HOUR_OF_DAY, Hour(i+1));
        Cal.get(i).set(Calendar.MINUTE, Min(i+1));
        Cal.get(i).set(Calendar.SECOND, 0);
    }


    //Intents
    Intent setVibration = new Intent();
    setVibration.setClass(this, AlarmReciever.class);



    //PENDING INTENTS
    ArrayList <PendingIntent> Lab_V= new ArrayList <PendingIntent>(60);


    for(int i=0; i<60; i++)
    {
        Lab_V.add(PendingIntent.getBroadcast(this, i, setVibration,
                PendingIntent.FLAG_UPDATE_CURRENT));

    }

    // create the object
    AlarmManager mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    for(int i=0; i<60; i++)
    {
            if(l.get(i).isChecked()){

             mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Cal.get(i).getTimeInMillis(), 7*24*60*60*1000, Lab_V.get(i));


            }
    }

    }


public void onClick(View view) {

    for(int i = 0; i < 60 ; i++){

        if(l.get(i).isChecked()){
            mPrefsEditor_l.putBoolean("lab"+i, true);
            mPrefsEditor_l.commit();
        }
        else {
            mPrefsEditor_l.putBoolean("lab"+i, false);
            mPrefsEditor_l.commit();
        }
    }

    getdata(view);

}
}

AlarmReciever.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.widget.Toast;

public class AlarmReciever extends BroadcastReceiver
{
private AudioManager mAudioManager;

@Override
        public void onReceive(Context context, Intent intent)
        {
                // TODO Auto-generated method stub

        mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
        mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

        // Show the toast
        Toast.makeText(context, "Vibration Mode", Toast.LENGTH_SHORT).show();

          }
}

Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ish.k.silentmode"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

  <!-- permission required to use Alarm Manager -->
        <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>




<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >



    <activity
        android:name="ish.k.silentmode.MainActivity"
        android:label="@string/app_name" 
       >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity
        android:name="ish.k.silentmode.GetSlots"
        android:label="Select your slots"
        android:screenOrientation="portrait" >


    </activity>

     <activity
        android:name="ish.k.silentmode.MorningSlots"
        android:label="Select your morning slots"
        android:screenOrientation="portrait" >


    </activity>

      <activity
        android:name="ish.k.silentmode.EveningSlots"
        android:label="Select your evening slots"
        android:screenOrientation="portrait" >


    </activity>

       <activity
        android:name="ish.k.silentmode.Labs"
        android:label="Select your lab slots"
        android:screenOrientation="portrait" >


    </activity>

       <activity
        android:name="ish.k.silentmode.About"
        android:label="About"
        android:screenOrientation="portrait" >


    </activity>

     <activity
        android:name="ish.k.silentmode.AlarmReciever"
        android:label="AlarmReciever" >


    </activity>

       <!-- Register the Alarm Receiver -->
               <receiver android:name=".AlarmReciever"/>

      <activity
        android:name="ish.k.silentmode.RingerMode"
        android:label="RingerMode" >


    </activity>

       <!-- Register the Alarm Receiver -->
               <receiver android:name=".RingerMode"/>
</application>

</manifest>

请帮忙。

提前致谢。

1 个答案:

答案 0 :(得分:1)

您需要在系统重新启动后再次设置它,您可以使用启动接收器。

Google开发者here对此进行了描述。

基本上,在AndroidManifest上定义接收器:

<receiver
    android:name=".BootReceiver"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
    android:enabled="true" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

将BootReceiver实现为普通广播接收器。