安卓闹钟不工作

发布于 2024-10-31 11:41:41 字数 4367 浏览 3 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭ゆ眷念 2024-11-07 11:41:41

在清单中添加权限和服务。使用这个它运行不会出错。

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

<uses-permission android:name="com.android.alarm.permission.SET_ALARM" >
</uses-permission>

<application
    android:icon="@drawable/ic_launcher"
    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>

    <receiver
        android:name=".OnetimeAlarmReceiver"
        android:process=":remote" />
</application>

Add permission and service in you manifest. Use this it runs without error.

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

<uses-permission android:name="com.android.alarm.permission.SET_ALARM" >
</uses-permission>

<application
    android:icon="@drawable/ic_launcher"
    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>

    <receiver
        android:name=".OnetimeAlarmReceiver"
        android:process=":remote" />
</application>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文