本文介绍了其余的在Android应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建余,我已经试过这种code,但不能正常工作,如果我设置报警它没有给予任何回应
下面是MainActivity.java
包com.example.ltr_3.dtrem;
公共类MainActivity延伸活动{
TimePicker timePicker;
DatePicker的日期选择器;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
// ---按钮来查看---
按钮btnOpen =(按钮)findViewById(R.id.btnSetAlarm);
btnOpen.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
timePicker =(TimePicker)findViewById(R.id.timePicker);
日期选择器=(DatePicker的)findViewById(R.id.date_picker_selected_date);
// ---使用AlarmManager触发警报---
AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);
// ---获得当前的日期和时间---
台历挂历= Calendar.getInstance();
// ---设定闹钟时间触发---
calendar.set(Calendar.YEAR,datePicker.getYear());
calendar.set(Calendar.MONTH,datePicker.getMonth());
calendar.set(Calendar.DAY_OF_MONTH,datePicker.getDayOfMonth());
calendar.set(Calendar.HOUR_OF_DAY,timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE,timePicker.getCurrentMinute());
calendar.set(Calendar.SECOND,0);
// --- PendingIntent启动活动时,报警触发---
意图I =新的意向书(com.example.ltr_3.dtrem.DisplayNotification);
// ---指定为1的ID ---
i.putExtra(NotifID,1);
PendingIntent displayIntent = PendingIntent.getActivity(
getBaseContext(),0,I,0);
// ---设置报警触发---
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),displayIntent);
}
});
}
}
MainActivity XML
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
<滚动型
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
<的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直>
<的TextView
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=你好/>
< DatePicker的机器人:ID =@ + ID / date_picker_selected_date
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT/>
< TimePicker机器人:ID =@ + ID / timePicker
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT/>
<按钮
机器人:ID =@ + ID / btnSetAlarm
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=设置报警/>
< / LinearLayout中>
< /滚动型>
< / LinearLayout中>
AlarmDetail.java
包com.example.ltr_3.dtrem;
进口android.app.Activity;
进口android.app.NotificationManager;
进口android.os.Bundle;
公共类AlarmDetails延伸活动{
@覆盖
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.alarmdetails);
// ---查找通知管理服务---
NotificationManager纳米=(NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
// ---取消通知---
nm.cancel(getIntent()getExtras()调用getInt(NotifID)。);
}
}
DisplayNotification.java
包com.example.ltr_3.dtrem;
进口android.app.Activity;
进口android.app.Notification;
进口android.app.NotificationManager;
进口android.app.PendingIntent;
进口android.content.Intent;
进口android.os.Bundle;
公共类DisplayNotification延伸活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
// ---获取通知的通知ID;
//由MainActivity传递---
INT notifID = getIntent()getExtras()调用getInt(NotifID)。。;
// --- PendingIntent发起活动,如果用户选择
//通知---
意图I =新的意向书(com.example.ltr_3.dtrem.AlarmDetails);
i.putExtra(NotifID,notifID);
PendingIntent detailsIntent =
PendingIntent.getActivity(此,0,I,0);
NotificationManager纳米=(NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
注意notif =新的通知(
R.drawable.icon,
时间到!,
System.currentTimeMillis的());
从CharSequence的=AlarmManager - 时间到了!
CharSequence的消息=这是您的警报,礼貌AlarmManager的;
notif.setLatestEventInfo(这一点,从,消息,detailsIntent);
// --- 100ms的延迟,振动的250毫秒,暂停100毫秒,
//然后振动500毫秒---
notif.vibrate =新长[] {100,250,100,500};
nm.notify(notifID,notif);
// ---破坏活动---
完();
}
}
解决方案
尝试以下链接它可以帮助你。
使用报警管理器调用该服务。
唐,别忘了拨打服务机器人的manifest.xml
notification特定的时间
i want to create remainder, i have tried this code but is not working properly if i am setting alarm its not giving any response
here is MainActivity.java
package com.example.ltr_3.dtrem;
public class MainActivity extends Activity {
TimePicker timePicker;
DatePicker datePicker;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//---Button view---
Button btnOpen = (Button) findViewById(R.id.btnSetAlarm);
btnOpen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
timePicker = (TimePicker) findViewById(R.id.timePicker);
datePicker = (DatePicker) findViewById(R.id.date_picker_selected_date);
//---use the AlarmManager to trigger an alarm---
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//---get current date and time---
Calendar calendar = Calendar.getInstance();
//---sets the time for the alarm to trigger---
calendar.set(Calendar.YEAR, datePicker.getYear());
calendar.set(Calendar.MONTH, datePicker.getMonth());
calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
calendar.set(Calendar.SECOND, 0);
//---PendingIntent to launch activity when the alarm triggers---
Intent i = new Intent("com.example.ltr_3.dtrem.DisplayNotification");
//---assign an ID of 1---
i.putExtra("NotifID", 1);
PendingIntent displayIntent = PendingIntent.getActivity(
getBaseContext(), 0, i, 0);
//---sets the alarm to trigger---
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), displayIntent);
}
});
}
}
MainActivity xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello" />
<DatePicker android:id="@+id/date_picker_selected_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TimePicker android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnSetAlarm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set Alarm" />
</LinearLayout>
</ScrollView>
</LinearLayout>
AlarmDetail.java
package com.example.ltr_3.dtrem;
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
public class AlarmDetails extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.alarmdetails);
//---look up the notification manager service---
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
//---cancel the notification---
nm.cancel(getIntent().getExtras().getInt("NotifID"));
}
}
DisplayNotification.java
package com.example.ltr_3.dtrem;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
public class DisplayNotification extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//---get the notification ID for the notification;
// passed in by the MainActivity---
int notifID = getIntent().getExtras().getInt("NotifID");
//---PendingIntent to launch activity if the user selects
// the notification---
Intent i = new Intent("com.example.ltr_3.dtrem.AlarmDetails");
i.putExtra("NotifID", notifID);
PendingIntent detailsIntent =
PendingIntent.getActivity(this, 0, i, 0);
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(
R.drawable.icon,
"Time's up!",
System.currentTimeMillis());
CharSequence from = "AlarmManager - Time's up!";
CharSequence message = "This is your alert, courtesy of the AlarmManager";
notif.setLatestEventInfo(this, from, message, detailsIntent);
//---100ms delay, vibrate for 250ms, pause for 100 ms and
// then vibrate for 500ms---
notif.vibrate = new long[] { 100, 250, 100, 500};
nm.notify(notifID, notif);
//---destroy the activity---
finish();
}
}
解决方案
Try Following links it may help you
use alarm manager calling the service.
Don,t forget to call the service in android manifest.xml
这篇关于其余的在Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!