2

我正在使用Alarm Manager。我使用了一項服務來啓動,並會響起一個聲音。但該服務並未開始。任何人都可以提出有關服務,待定意圖和識別我的代碼中的錯誤。代碼從網上覆制。Android AlarmManager使用鈴聲啓動服務

這是我AndroidScheduledActivity.java

public class AndroidScheduledActivity extends Activity { 

    private PendingIntent pendingIntent; 

    /** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 

     Button buttonStart = (Button) findViewById(R.id.startalarm); 

     Button buttonCancel = (Button) findViewById(R.id.cancelalarm); 

     buttonStart.setOnClickListener(new Button.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       // TODO Auto-generated method stub 

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

       pendingIntent = PendingIntent.getService(AndroidScheduledActivity.this, 
         0, myIntent, 0); 

       AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

       Calendar calendar = Calendar.getInstance(); 

       calendar.setTimeInMillis(System.currentTimeMillis()); 

       calendar.add(Calendar.SECOND, 10); 

       alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
         pendingIntent); 

       Toast.makeText(AndroidScheduledActivity.this, "Start Alarm", 
         Toast.LENGTH_LONG).show(); 
       Log.d("Alarm", "Alarm Started"); 

      } 
     }); 

     buttonCancel.setOnClickListener(new Button.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       // TODO Auto-generated method stub 

       AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

       alarmManager.cancel(pendingIntent); 

       // Tell the user about what we did. 

       Toast.makeText(AndroidScheduledActivity.this, "Cancel!", 
         Toast.LENGTH_LONG).show(); 

      } 
     }); 

    } 
} 

這是我MyAlarmService.java

public class MyAlarmService extends Service { 

    @Override 
    public void onCreate() { 

     // TODO Auto-generated method stub 

     Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show(); 

    } 

    @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(); 

     Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show(); 

    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     Log.d("AlarmService", "Alarm Starting to ring"); 

     Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     MediaPlayer mp = MediaPlayer.create(getBaseContext(), alert); 
     mp.setVolume(100, 100); 
     mp.start(); 
     mp.setOnCompletionListener(new OnCompletionListener() { 

      @Override 
      public void onCompletion(MediaPlayer mp) { 
       mp.release(); 
      } 
     }); 

     Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); 
     vibrator.vibrate(400); 

     Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show(); 

     return Service.START_STICKY; 

    } 

    @Override 
    public boolean onUnbind(Intent intent) { 

     // TODO Auto-generated method stub 

     Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show(); 

     return super.onUnbind(intent); 

    } 

} 

而且我menifest.xml文件

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

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

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.alarm_basic.AndroidScheduledActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

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

</manifest> 
+0

您的應用程序崩潰,我的意思是你得到任何異常,當觸發報警?如果您的應用程序確實崩潰,請在此提供LogCat,以便我們可以清楚地瞭解它爲何發生。 – saiful103a

+0

不,我的應用程序在測試時沒有崩潰。 我認爲我的問題已解決 – Mazhar

回答

1
Intent intent = new Intent(DashboardScreen.this, ServiceClass.class); 
PendingIntent pintent = PendingIntent.getService(DashboardScreen.this, 0, intent, 0); 
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent); 

試試吧,並讓我知道發生了什麼...

在你manifest.xml文件

<service android:enabled="true" android:name=".ServiceClass" /> 
1
  1. 添加權限清單檔案中的

    < uses-permission android:name="android.permission.VIBRATE" /> 
    
  2. 應檢查振動器爲空或不是。

    Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);    
    if (vibrator != null) 
        vibrator.vibrate(400); 
    
+0

謝謝解決一些問題。 但我的主要問題不是。所以,我決定選擇Hardik的答案。 – Mazhar

3

還需要檢查wheather鈴聲爲空也。

Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
Ringtone r = RingtoneManager.getRingtone(getBaseContext(), alert); 

if(r == null){ 

    alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    r = RingtoneManager.getRingtone(getBaseContext(), alert); 

    if(r == null){ 
     alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); 
     r = RingtoneManager.getRingtone(getBaseContext(), alert);    
    } 
} 
if(r != null) 
    r.play(); 

或者otherway

MediaPlayer mp = MediaPlayer.create(getBaseContext(), alert); 
    if(mp !=null) {  
     mp.setVolume(100, 100); 
     mp.start(); 
     mp.setOnCompletionListener(new OnCompletionListener() { 

      @Override 
      public void onCompletion(MediaPlayer mp) { 
       mp.release(); 
      } 
     }); 
    }