我知道这个问题已经问过,但我真的很惊讶,如此努力之后。我还没有启用的Android模拟器的工作报警。我已经尝试了所有$ C $的CS这个论坛,但徒劳无功。即使我下载一个示例code仍然没有奏效。请大家帮我,大家好我送你我的所有code。谁能告诉我在Android模拟器做响铃警告?如果是的话那么是什么问题,我的code?我测试了同样的code的API 4级和10这两个没有工作。即使我创建2个不同的项目,但徒劳无功。
AndroidAlarmService:的
包com.exercise.AndroidAlarmService; 进口的java.util.Calendar; 进口android.app.Activity; 进口android.app.AlarmManager; 进口android.app.PendingIntent; 进口android.content.Intent; 进口android.os.Bundle; 进口android.view.View; 进口android.widget.Button; 进口android.widget.Toast; 公共类AndroidAlarmService延伸活动{ 私人PendingIntent pendingIntent; / **第一次创建活动时调用。 * / @覆盖 公共无效的onCreate(包savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.main); 按钮buttonStart =(按钮)findViewById(R.id.startalarm); 按钮buttonCancel =(按钮)findViewById(R.id.cancelalarm); buttonStart.setOnClickListener(新Button.OnClickListener(){ 公共无效的onClick(查看为arg0){ // TODO自动生成方法存根 意图myIntent =新的意图(AndroidAlarmService.this,MyAlarmService.class); pendingIntent = PendingIntent.getService(AndroidAlarmService.this,0,myIntent,0); AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE); 台历挂历= Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis的()); calendar.add(Calendar.SECOND,10); alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent); Toast.makeText(AndroidAlarmService.this,启动报警,Toast.LENGTH_LONG).show(); }}); buttonCancel.setOnClickListener(新Button.OnClickListener(){ 公共无效的onClick(查看为arg0){ // TODO自动生成方法存根 AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE); alarmManager.cancel(pendingIntent); //告诉我们所做的用户。 Toast.makeText(AndroidAlarmService.this,取消!,Toast.LENGTH_LONG).show(); }}); } }
MyAlarmService:的
包com.exercise.AndroidAlarmService; 进口android.app.Service; 进口android.content.Intent; 进口android.os.IBinder; 进口android.widget.Toast; 公共类MyAlarmService延伸服务{ @覆盖 公共无效的onCreate(){ // TODO自动生成方法存根 Toast.makeText(此,MyAlarmService.onCreate(),Toast.LENGTH_LONG).show(); } @覆盖 公众的IBinder onBind(意向意图){ // TODO自动生成方法存根 Toast.makeText(此,MyAlarmService.onBind(),Toast.LENGTH_LONG).show(); 返回null; } @覆盖 公共无效的onDestroy(){ // TODO自动生成方法存根 super.onDestroy(); Toast.makeText(此,MyAlarmService.onDestroy(),Toast.LENGTH_LONG).show(); } @覆盖 公共无效ONSTART(意向意图,诠释startId){ // TODO自动生成方法存根 super.onStart(意向,startId); Toast.makeText(此,MyAlarmService.onStart(),Toast.LENGTH_LONG).show(); } @覆盖 公共布尔onUnbind(意向意图){ // TODO自动生成方法存根 Toast.makeText(此,MyAlarmService.onUnbind(),Toast.LENGTH_LONG).show(); 返回super.onUnbind(意向); } }
布局:的
< XML版本=1.0编码=UTF-8&GT?; < LinearLayout中的xmlns:机器人=http://schemas.android/apk/res/android 机器人:方向=垂直 机器人:layout_width =FILL_PARENT 机器人:layout_height =FILL_PARENT > <的TextView 机器人:layout_width =FILL_PARENT 机器人:layout_height =WRAP_CONTENT 机器人:文本=@字符串/你好 /> <按钮 机器人:ID =@ + ID / startalarm 机器人:layout_width =FILL_PARENT 机器人:layout_height =WRAP_CONTENT 机器人:文本=开始 /> <按钮 机器人:ID =@ + ID / cancelalarm 机器人:layout_width =FILL_PARENT 机器人:layout_height =WRAP_CONTENT 机器人:文本=取消 /> < / LinearLayout中>
Android清单:的
< XML版本=1.0编码=UTF-8&GT?; <舱单的xmlns:机器人=http://schemas.android/apk/res/android 包=com.exercise.AndroidAlarmService 安卓版code =1 机器人:VERSIONNAME =1.0> <使用-SDK安卓的minSdkVersion =4/> <应用机器人:图标=@可绘制/图标机器人:标签=@字符串/ APP_NAME> <活动机器人:名称=。AndroidAlarmService 机器人:标签=@字符串/ APP_NAME> <意向滤光器> <作用机器人:名称=android.intent.action.MAIN/> <类机器人:名称=android.intent.category.LAUNCHER/> &所述; /意图滤光器> < /活性GT; <服务机器人。MyAlarmServiceNAME = /> < /用途> < /舱单>解决方案
我也得到同样的问题。但我安装我的应用程序在移动则警钟正在发挥作用。
后来我才知道警铃不工作的模拟器。因此,没有必要担心这一点。你必须尝试您的手机上。它的工作的罚款。
I know this question has asked before, but I am really surprised, after trying so hard. I have not enabled to work alarm on android emulator. I have tried all codes of this forum but in vain. Even I download a sample code still it did not work. please help me guys I am sending you my all code. Can anyone tell me "do alarm bell rings on android emulator"? If yes then what is problem in my code? I tested same code for API level 4 and 10. Both did not work. Even I create 2 different projects but in vain.
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); } }Layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="schemas.android/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 manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="schemas.android/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>解决方案
I was also getting same problem. But I installed my application on mobile then the alarm bell was working.
Then I came to know alarm bell is not working on emulator. So no need to worry about this. You have to try on your mobile. Its working fine.
更多推荐
如何设置在Android的Java的警报
发布评论