Alarmmanager有时在设备上工作

时间:2015-09-10 10:21:59

标签: android alarmmanager

alarmmanager在模拟器上运行得很好,但在物理设备上却没有。 它有时在设备上运行。请帮忙。我无法找到它为什么不在设备上工作。有bug吗? 相同的代码在之前工作正常。

  <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wim.wimonandroid"
android:versionCode="94"
android:versionName="10.1" >

<uses-sdk android:minSdkVersion="10" />

<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name" >
    <activity
        android:screenOrientation="portrait" 
        android:name=".wimOnAndroid"
        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=".Compas"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name=".TutorialZoomActivity1"
        android:label="@string/ramadan" >
    </activity>
    <activity
        android:name=".AlarmListView"
        android:label="@string/app_name3" >
    </activity>
    <activity
        android:name=".wimRss"
        android:label="@string/app_name1" >
    </activity>
    <activity
        android:name=".CityListView"
        android:label="@string/app_name4" >
    </activity>
    <activity
        android:name=".jummah"
        android:label="@string/jummah" >
    </activity>
    <activity
        android:name=".settings"
        android:label="@string/settings" >
    </activity>
    <activity
        android:name=".QA"
        android:label="@string/QA" >
    </activity>
    <activity
        android:name=".SendEmailActivity"
        android:label="@string/app_name_Sendmail" >
    </activity>
    <activity
        android:name=".Touch"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".compass"
        android:label="@string/compass" >
    </activity>
    <activity
        android:screenOrientation="landscape" 
        android:configChanges="orientation|keyboardHidden"
        android:name=".sehri"
        android:label="sehri" >
    </activity>
    <activity
        android:screenOrientation="landscape" 
        android:configChanges="orientation|keyboardHidden"
        android:name=".iftari"
        android:label="iftari" >
    </activity>
    <activity
        android:name=".felles"
        android:label="felles" >
    </activity>

    <!-- <service android:enabled="true" android:name=".AlarmService" /> -->
    <service android:name=".MyAlarmService" >
    </service>
    <service android:name=".UpdateWidgetService" >
    </service>


    <!-- Broadcast Receiver that will process AppWidget updates -->

    <receiver android:name=".MyBroadcastreceiver" >
        <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>

        <intent-filter>
        <action android:name="android.intent.action.PACKAGE_REPLACED"/>
        <data android:scheme="package"  />
        </intent-filter> 
    </receiver>

    <receiver
        android:name=".wimWidget"
        android:label="@string/app_nameWid" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/hello_widget_provider" />
    </receiver>

    <activity
        android:name=".SehriActivity"
        android:label="@string/title_activity_sehri" >
    </activity>
</application>

    package com.wim.wimonandroid;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Calendar;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.IBinder;
import android.os.Vibrator;
import android.util.Log;
import android.widget.Toast;

public class MyAlarmService extends Service {
    MediaPlayer player;
    String readString = new String("");
    String readAlarm = new String("");
    String readSpilt = new String("");
    String readForste = new String("");
    private PendingIntent pendingIntent;
    private   AlarmManager alarmManager;

@Override
public void onCreate() {

    Log.v("tid", "MyAlarmService - onCreate()" );

    try {
        readFile(); //sjekker om Alarm er slått på eller ei

    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try{
        hent(); //setter neste alarm
    } catch (IOException e) {

    e.printStackTrace();
}
    if(readString.equals("2")  ){ 
    Log.v("tid", "MyAlarmService - onCreate - Kaller onDestroy()" );
    onDestroy() ; 
    }
}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
//Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
return null;
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();

Log.v("tid", "MyAlarmService - onDestroy");
//writeFile(2);
}



public int onStartCommand(Intent intent, int flags, int startId) {
    Log.v("tid", "MyAlarmService - onStartCommand()");

        String forste = new String(""); 

        try {
            readFile(); // Sjekker om alarm er på eller ei.

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }


    try {
        forste = readForste();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
        Calendar cal = Calendar.getInstance();
        int hour = cal.get(Calendar.HOUR_OF_DAY) ; //1300
        int minutes = cal.get(Calendar.MINUTE); 

        Log.v("tid", "MyAlarmService - onStartCommand - readAlarm: " + 
 readAlarm);    


        AudioManager am = 
(AudioManager)getSystemService(Context.AUDIO_SERVICE);


        switch (am.getRingerMode()) {
            case AudioManager.RINGER_MODE_SILENT:
                Log.v("tid","Silent mode");
                break;
            case AudioManager.RINGER_MODE_VIBRATE:
                Log.v("tid","Vibrate mode");
                break;
            case AudioManager.RINGER_MODE_NORMAL:
                Log.v("tid","Normal mode - onStart");

            //  if(Integer.parseInt(readAlarm.trim()) == 0){                
                if(readAlarm.equals("0")){              

                Log.i("tid","Normal mode - door - Minutter: " + minutter);
                            am.setStreamVolume(AudioManager.STREAM_MUSIC,

  am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),   
 AudioManager.FLAG_SHOW_UI);
                            player = MediaPlayer.create(this, R.raw.door);
                            player.setLooping(false); // Set looping
                            player.setVolume(1, 1);
                            player.start(); 
                            player.release();
                            Runnable r = new Runnable() {
                                    public void run(){

                                    }
                                };

                                Handler h = new Handler();
                                h.postDelayed(r, 60000);

                                //alarmHjelp.restart();

                }
                //else if(Integer.parseInt(readAlarm.trim()) == 1){
                else if(readAlarm.equals("1")){
                    Log.i("tid","Normal mode - azaan");
                        am.setStreamVolume(AudioManager.STREAM_MUSIC,
                        am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),  
                        AudioManager.FLAG_SHOW_UI);

                        player = MediaPlayer.create(this, R.raw.azan);
                        player.setLooping(false); // Set looping
                        player.setVolume(1, 1);
                        player.start(); 
                       player.release();
                        Runnable r = new Runnable() {
                                public void run(){

                                }
                            };

                            Handler h = new Handler();
                            h.postDelayed(r, 60000);
                }

                break;
        }


        if(readAlarm.equals("0") || readAlarm.equals("1")){
            Log.i("tid","vibrasjon");
            Vibrator v = (Vibrator)      
         getSystemService(Context.VIBRATOR_SERVICE);
            v.vibrate(4000);
            Toast.makeText(getApplicationContext(), "Tid for bønn..." ,
                Toast.LENGTH_LONG).show();      


        }

    }       
        try {
            hent(); //setter neste alarm
        } catch (IOException e) {

            e.printStackTrace();
        }
//} // 2 er lydløs



        Log.v("tid","MyAlarmService - OnStartCommand - Start sticky");
        return START_STICKY;
}


public void hent() throws IOException{
    Log.v("tid", "MyAlarmService - hent()");

    int alarmH = 0;                                 
    int alarmM = 0;

    Calendar cal2 = Calendar.getInstance();
    int hour = cal2.get(Calendar.HOUR_OF_DAY) ; 
    int minutes = cal2.get(Calendar.MINUTE);    

    try {
        checkBy = readCity();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String tider = new String("");

    Log.v("tid", "MyAlarmService - hent() - By: " + checkBy);

     //Setting alarm every second hour
    alarmH = alarmH + 2;


        Intent myIntent = new Intent(this, MyAlarmService.class);

        pendingIntent = PendingIntent.getService(this,  0, myIntent, 0);
        alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

        Calendar cal = Calendar.getInstance();

        if(nesteDag == 1)
            {
                 Log.v("tid",  "MyAlarmService - hent() - nestedag");
                cal.add( Calendar.DATE, 1 );
            }

        cal.set(Calendar.HOUR_OF_DAY, alarmH);
        cal.set(Calendar.MINUTE, alarmM);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 
 pendingIntent);
        Log.i("tid", "MyAlarmService - hent() - Neste alarm: " + alarmH + 
 ":" + alarmM);



 }
}

public void readFile() throws IOException{

    Log.v("tid", "MyAlarmService - readFile() - readAlarm: " + readAlarm);

        // open the file for reading
        InputStream instream = openFileInput("alarm.txt");

        InputStreamReader inputreader = new InputStreamReader(instream);
        BufferedReader buffreader = new BufferedReader(inputreader);

        while (( readAlarm = buffreader.readLine()) != null) {
            Log.v("tid", "readAlarm: " + readAlarm);

            break;
        }
        instream.close();

        if(readAlarm == null){
            Log.v("tid", "MyAlarmService - readFile - readAlarm er NULL");
            readAlarm = "2";
            writeFile(2);
            Log.v("tid", "MyAlarmService - readFile - readAlarm (ny verdi): 
 " + readAlarm);

        }

 }  

  public void onBackPressed()  
    {  
            this.startActivity(new Intent(this,wimOnAndroid.class));

    }

@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
return super.onUnbind(intent);
}

}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
 }

0 个答案:

没有答案