我正在使用TimePicker从用户那里获取特定时间。我正在利用这段时间每天在这个时间设置重复闹钟。当闹钟响起时,我想要将通知发送给用户。我的代码似乎是正确的,我没有在Android工作室中得到任何错误,但当我运行这个应用程序并在特定时间设置它...它永远不会关闭。请帮忙。此外,我还没有找到任何能告诉我如何使用TimePicker选择AM或PM用户的东西。我的代码如下。提前谢谢。
这是MyActivity(在发布时打开的那个)
public class MyActivity extends Activity {
TimePicker mTimePicker;
Button setAlarm;
private int hour;
private int minute;
PendingIntent mPendingIntent;
int AM_PM;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
setAlarm = (Button) findViewById(R.id.setUpAlarm);
mTimePicker = (TimePicker) findViewById(R.id.timePicker);
setAlarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setAlarm();
}
});
}
private void setAlarm() {
hour = mTimePicker.getCurrentHour();
minute = mTimePicker.getCurrentMinute();
Intent intent = new Intent(this, NotifyService.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
mPendingIntent = PendingIntent.getService(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND , 0 );
calendar.set(Calendar.MINUTE , 0 + minute);
calendar.set(Calendar.HOUR , 0 + hour);
calendar.set(Calendar.AM_PM , Calendar.PM);
Toast.makeText(this, calendar.get(Calendar.MINUTE) + " " + calendar.get(Calendar.HOUR), Toast.LENGTH_SHORT).show();
// * 60 * 60 * 24
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP , calendar.getTimeInMillis() , 1000 * 60 * 60 * 24 , mPendingIntent);
// Toast.makeText(MyActivity.this , "Alarm Set" , Toast.LENGTH_SHORT).show();
}
这是我的通知类
public class NotifyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this.getApplicationContext() , MyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent ,0 );
Notification mNotify = new Notification.Builder(this)
.setContentTitle("Come Back!")
.setContentText("Have you seen todays tip?")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.setSound(sound)
.build();
mNM.notify( 1 , mNotify);
}
}
我的清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidy.notificationapp" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:3)
您必须在清单中声明您的服务,否则无法启动。
此外,getCurrentHour()始终以24小时格式返回小时,无需知道用户是在AM还是PM中输入。
您可能还想考虑将服务中的所有工作从onCreate()移到onStartCommand(),因为只有当您的服务尚未存在时才会调用onCreate(),因为您可以似乎要阻止它(你也可以考虑做)。