android闹钟无法正常工作

时间:2011-04-07 07:07:43

标签: android alarm

我在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

 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">

    

我错了 任何人都可以帮助你,谢谢你

1 个答案:

答案 0 :(得分:0)

在清单中添加权限和服务。使用它运行没有错误。

<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>