1

我真的很陌生,我一直在做一個應用程序,根據我的大學時間表將手機變成振動模式。我希望我的應用程序能夠在手機重新啓動後記住待處理的意圖,以便在每次重新啓動設備時都不必向其時間表提供數據。如何在手機重新啓動後記住掛起的意圖和警報?

這裏是我的代碼:

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清單:

<?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

系統重啓後需要重新設置,可以使用啓動接收器。

它在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作爲一個正常的廣播接收器。