如何在android java中设置闹钟

时间:2011-09-03 20:11:00

标签: android alarm

我知道这个问题之前曾经问过,但是在努力之后我真的很惊讶。我没有启用Android模拟器上的警报。我已经尝试过这个论坛的所有代码但是徒劳无功。即使我下载示例代码仍然无法正常工作。请帮助我们,我发送给你我所有的代码。任何人都可以告诉我“在Android模拟器上做警钟响”吗?如果是,那么我的代码中有什么问题?我为API级别4和10测试了相同的代码。两者都不起作用。即使我创建了两个不同的项目,但都是徒劳的。

AndroidAlarmService:

package com.exercise.AndroidAlarmService;

import java.util.Calendar;

import android.app.Activity;     
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AndroidAlarmService extends Activity {

private PendingIntent pendingIntent;


/** Called when the activity is first created. */
@Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 Button buttonStart = (Button)findViewById(R.id.startalarm);
 Button buttonCancel = (Button)findViewById(R.id.cancelalarm);

 buttonStart.setOnClickListener(new Button.OnClickListener(){


 public void onClick(View arg0) {
   // TODO Auto-generated method stub

  Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class);
   pendingIntent = PendingIntent.getService(AndroidAlarmService.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(AndroidAlarmService.this, "Start Alarm", Toast.LENGTH_LONG).show();
  }});

 buttonCancel.setOnClickListener(new Button.OnClickListener(){


   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(AndroidAlarmService.this, "Cancel!", Toast.LENGTH_LONG).show();


    }});

    }
    }

MyAlarmService:

 package com.exercise.AndroidAlarmService;

 import android.app.Service;
 import android.content.Intent;
 import android.os.IBinder;
 import android.widget.Toast;

 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 void onStart(Intent intent, int startId) {
 // TODO Auto-generated method stub
 super.onStart(intent, startId);
 Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
 }

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

 }

布局:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
   <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
 />
 <Button
    android:id="@+id/startalarm"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Start"
 />
 <Button
    android:id="@+id/cancelalarm"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Cancel"
  />
  </LinearLayout>

Android清单:

 <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.exercise.AndroidAlarmService"
    android:versionCode="1"
    android:versionName="1.0">
   <uses-sdk android:minSdkVersion="4" />

   <application android:icon="@drawable/icon" android:label="@string/app_name">
       <activity android:name=".AndroidAlarmService"
              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>

1 个答案:

答案 0 :(得分:3)

我也遇到了同样的问题。但我在移动设备上安装了我的应用程序,然后警铃正在工作。

然后我才知道闹铃不能在模拟器上运行。所以不用担心这个。你必须试试你的手机。工作正常。