如何在Android中设置报警?如何在、Android

2023-09-11 20:36:13 作者:混沌ン帝国

以下是我的code任何机构可以请你告诉我,为什么它不工作。我已经学会从本教程。但它不工作的任何帮助将AP preciable。

我的code是

 进口java.util.Calendar中;

进口android.app.Activity;
进口android.app.AlarmManager;
进口android.app.ListActivity;
进口android.app.Notification;
进口android.app.NotificationManager;
进口android.app.PendingIntent;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.widget.ArrayAdapter;
进口android.widget.Button;
进口android.widget.ListView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.Toast;


公共类通知延伸活动{
   按钮BTN;

/ **第一次创建活动时调用。 * /
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

   的setContentView(R.layout.nit);
   日历CAL = Calendar.getInstance();
   cal.set(Calendar.MONTH,6);
   cal.set(Calendar.YEAR,2011);
   cal.set(Calendar.DAY_OF_MONTH,29);
   cal.set(Calendar.HOUR_OF_DAY,17);
   cal.set(Calendar.MINUTE,30);

  //的String []花花公子=新的String [] {尼廷,AVI,阿曼,拉胡尔,pattrick,minkle,曼莫汉,尼廷,尼廷};

    // setListAdapter(新ArrayAdapter<字符串>(这一点,android.R.layout.simple_expandable_list_item_1,纨绔子弟));
   // getListView()setTextFilterEnabled(真)。

    //的String [] dude1 =新的String [] {尼廷,AVI,阿曼,拉胡尔,pattrick,minkle,曼莫汉};

    意向意图=新的意图(这一点,Mote.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(),1253年,意图,PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);

    AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);

    alarmManager.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pendingIntent);
    Toast.makeText(这一点,报警的工作。,Toast.LENGTH_LONG).show();
}
 

}

我的接收器类

 进口android.app.Notification;
进口android.app.NotificationManager;
进口android.app.PendingIntent;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.widget.Toast;

公共类莫特扩展的BroadcastReceiver {

公共无效的onReceive(上下文的背景下,意图意图){
    // TODO自动生成方法存根

    Toast.makeText(背景下,报警的工作。,Toast.LENGTH_LONG).show();
    INT图标= R.drawable.icon;
     CharSequence的tickerText =你好,你吃药,我尼廷·夏尔马;
        时长= System.currentTimeMillis的();

    //通知的通知=新的通知(图标,tickerText时);

    CharSequence的contentTitle =我的通知;
    CharSequence的contentText =的Hello World!;


    //notification.setLatestEventInfo(context,contentTitle,contentText,contentIntent);
    最终诠释NOTIF_ID = 1234;
    NotificationManager notofManager =(NotificationManager)上下文。 getSystemService(Context.NOTIFICATION_SERVICE);
  //通知注=新的通知(R.drawable.face,新活动,System.currentTimeMillis的());
    意图notificationIntent =新的意图(背景下,Alset.class);
    PendingIntent contentIntent = PendingIntent.getActivity(上下文,0,notificationIntent,0);
    通知通知=新的通知(图标,tickerText时);
    //通知notification1 =新的通知(R.drawable.icon,醒醒报警,System.currentTimeMillis的());
    notification.setLatestEventInfo(背景下,contentTitle,contentText,contentIntent);
    notification.flags = Notification.FLAG_INSISTENT;
    notification.defaults | = Notification.DEFAULT_SOUND;
    //notification.setLatestEventInfo(context,我的活动,这将运行在按一下按钮,contentIntent);
    notofManager.notify(NOTIF_ID,通知);

    // PendingIntent contentIntent = PendingIntent.getActivity(上下文,0,新意图(上下文,MainActivity.class),0);
    //notification.setLatestEventInfo(context,上下文名称,语境下的文本,contentIntent);
   // notification.flags = Notification.FLAG_INSISTENT;
}



}
 

解决方案

我想你想设置的报警6月26日,而不是7月26日。如果是的话,更改 cal.set(Calendar.MONTH,6); cal.set(Calendar.MONTH,5); ,因为月份是从零开始的。如果你打算报警火的7月26日则预计报警触发时的日期,时间为26日2011年7月,17:30

android中怎么设置组件在LinearLayout中居中

Following is my code can Any body please tell me why it is not working .I have learned it from this tutorial. But it is not working any help will be appreciable.

My code is

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.ListActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;


public class Notify extends Activity {
   Button btn;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

   setContentView(R.layout.nit);
   Calendar cal=Calendar.getInstance();
   cal.set(Calendar.MONTH,6);
   cal.set(Calendar.YEAR,2011);
   cal.set(Calendar.DAY_OF_MONTH,29);
   cal.set(Calendar.HOUR_OF_DAY,17);
   cal.set(Calendar.MINUTE,30);

  //  String[] dude=new String[] {"nitin","avi","aman","rahul","pattrick","minkle","manmohan","nitin","nitin"};

    //setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,dude));
   // getListView().setTextFilterEnabled(true);

    //String[] dude1=new String[] {"nitin","avi","aman","rahul","pattrick","minkle","manmohan"};

    Intent intent = new Intent(this, Mote.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 1253, intent, PendingIntent.FLAG_UPDATE_CURRENT|  Intent.FILL_IN_DATA);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),pendingIntent );
    Toast.makeText(this, "Alarm worked.", Toast.LENGTH_LONG).show();
}

}

and my Receiver class is

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class Mote extends BroadcastReceiver{

public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
    int icon = R.drawable.icon;
     CharSequence tickerText = "Hello you have to take medicine I am Nitin Sharma";
        long when = System.currentTimeMillis();

    //Notification notification = new Notification(icon, tickerText,when );

    CharSequence contentTitle = "My notification";
    CharSequence contentText = "Hello World!";


    //notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    final int NOTIF_ID = 1234;
    NotificationManager notofManager = (NotificationManager)context. getSystemService(Context.NOTIFICATION_SERVICE);
  // Notification note = new Notification(R.drawable.face,"NEW ACTIVITY", System.currentTimeMillis());
    Intent notificationIntent = new Intent(context, Alset.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context,0, notificationIntent, 0);
    Notification notification = new Notification(icon, tickerText,when );
    //Notification notification1 = new Notification(R.drawable.icon, "Wake up alarm", System.currentTimeMillis());
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    notification.flags = Notification.FLAG_INSISTENT;
    notification.defaults |= Notification.DEFAULT_SOUND;
    //notification.setLatestEventInfo(context, "My Activity", "This will runs on button click", contentIntent);
    notofManager.notify(NOTIF_ID,notification);

    //PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
    //notification.setLatestEventInfo(context, "Context Title", "Context text", contentIntent);
   // notification.flags = Notification.FLAG_INSISTENT;
}



}

解决方案

I think you want to set the alarm for 26th June and not 26th July. If so then change cal.set(Calendar.MONTH,6); to cal.set(Calendar.MONTH,5); because the months are zero-based. if you intend the alarm to fire on 26th july then it is expected that the alarm will fire when the date-time is 26th July 2011, 17:30