安卓闹钟不工作
我在android中的一个示例的帮助下尝试了简单的闹钟...它运行良好,但我无法设置闹钟
logcat 中的错误:
04-08 00:17:52.016: DEBUG/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol
alarm.java
public class Alarm extends Activity
{
DateFormat fmtDateAndTime=DateFormat.getDateTimeInstance();
TextView dateAndTimeLabel;
Calendar dateAndTime=Calendar.getInstance();
Button Alarm;
Alarm instance;
Toast mToast=null;
DatePickerDialog.OnDateSetListener d=new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
dateAndTime.set(Calendar.YEAR, year);
dateAndTime.set(Calendar.MONTH, monthOfYear);
dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
}
};
TimePickerDialog.OnTimeSetListener t=new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay,int minute) {
dateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
dateAndTime.set(Calendar.MINUTE, minute);
view.setCurrentHour(Calendar.HOUR_OF_DAY);
}
};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
instance = this;
Button btn=(Button)findViewById(R.id.Date);
Alarm = (Button)findViewById(R.id.Alarm);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new DatePickerDialog(Alarm.this,d,
dateAndTime.get(Calendar.YEAR),
dateAndTime.get(Calendar.MONTH),
dateAndTime.get(Calendar.DAY_OF_MONTH)).show(); }
});
btn=(Button)findViewById(R.id.time);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new TimePickerDialog(Alarm.this,t,
dateAndTime.get(Calendar.HOUR_OF_DAY),
dateAndTime.get(Calendar.MINUTE),
true).show();
}
});
Alarm.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
Intent intent = new Intent(instance, OnetimeAlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(instance,0, intent, 0);
dateAndTime.add(Calendar.SECOND, 5);
long timeSet = dateAndTime.getTimeInMillis();
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, timeSet, sender);
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(instance, "Alarm Set for " + dateAndTime.getTime(),
Toast.LENGTH_LONG);
mToast.show();
}
});
}
}
OnetimeAlarmReceiver.java< /strong>
public class OnetimeAlarmReceiver extends Service{
@Override
public void onCreate() {
super.onCreate();
Builder builder = new Builder(this);
builder.setMessage("Started").create().show();
Log.d("Value ", "Service Started");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Builder builder = new Builder(this);
builder.setMessage("Started").create().show();
Log.d("Value ", "Service Started");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
清单:
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Alarm"
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 =".OnetimeAlarmReceiver">
我哪里做错了 任何人都可以帮忙谢谢
I tried simple alarm by the help of one example in android... its running good but i can not get alarm to the set the time
Error in logcat :
04-08 00:17:52.016: DEBUG/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol
alarm.java
public class Alarm extends Activity
{
DateFormat fmtDateAndTime=DateFormat.getDateTimeInstance();
TextView dateAndTimeLabel;
Calendar dateAndTime=Calendar.getInstance();
Button Alarm;
Alarm instance;
Toast mToast=null;
DatePickerDialog.OnDateSetListener d=new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
dateAndTime.set(Calendar.YEAR, year);
dateAndTime.set(Calendar.MONTH, monthOfYear);
dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
}
};
TimePickerDialog.OnTimeSetListener t=new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay,int minute) {
dateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
dateAndTime.set(Calendar.MINUTE, minute);
view.setCurrentHour(Calendar.HOUR_OF_DAY);
}
};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
instance = this;
Button btn=(Button)findViewById(R.id.Date);
Alarm = (Button)findViewById(R.id.Alarm);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new DatePickerDialog(Alarm.this,d,
dateAndTime.get(Calendar.YEAR),
dateAndTime.get(Calendar.MONTH),
dateAndTime.get(Calendar.DAY_OF_MONTH)).show(); }
});
btn=(Button)findViewById(R.id.time);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new TimePickerDialog(Alarm.this,t,
dateAndTime.get(Calendar.HOUR_OF_DAY),
dateAndTime.get(Calendar.MINUTE),
true).show();
}
});
Alarm.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
Intent intent = new Intent(instance, OnetimeAlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(instance,0, intent, 0);
dateAndTime.add(Calendar.SECOND, 5);
long timeSet = dateAndTime.getTimeInMillis();
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, timeSet, sender);
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(instance, "Alarm Set for " + dateAndTime.getTime(),
Toast.LENGTH_LONG);
mToast.show();
}
});
}
}
OnetimeAlarmReceiver.java
public class OnetimeAlarmReceiver extends Service{
@Override
public void onCreate() {
super.onCreate();
Builder builder = new Builder(this);
builder.setMessage("Started").create().show();
Log.d("Value ", "Service Started");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Builder builder = new Builder(this);
builder.setMessage("Started").create().show();
Log.d("Value ", "Service Started");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
manifest:
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Alarm"
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 =".OnetimeAlarmReceiver">
where i did mistake
Any one can help thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在清单中添加权限和服务。使用这个它运行不会出错。
Add permission and service in you manifest. Use this it runs without error.