每当应用程序运行或不运行时显示通知

2024-04-24

在我的程序中,无论应用程序是否运行,都必须激活通知。我应该将通知方法放在 onCreate() 中吗?我的通知就像闹钟一样。请稍微检查一下。

public String getCurrentTime(){
    Calendar c= Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
    String strDate = sdf.format(c.getTime());
    return strDate;
}

这是获取系统时间。

public void jsonen()
{
    int status=2;
    JSONObject json=null;

    String teamID=null;
    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    //postParameters.add(new BasicNameValuePair("email",edit_txt_EmailAddress.getText().toString()));//// define the parameter
    postParameters.add(new BasicNameValuePair("userID","396797666"));

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
       .detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode is most commonly used to catch accidental disk or network access on the application's main thread
       .penaltyLog().build());

    ArrayList<NameValuePair> pp = new ArrayList<NameValuePair>();

    String response = null;
    try {
          response=CustomHttpClient.executeHttpPost("http://10.0.2.2/football365/notification.php", postParameters);
         // json=new JSONObject(response);
        //  teamID=json.getString("teamID");
         // Log.i("Team ID",teamID+"");
        }
   catch (Exception e) {

        e.printStackTrace();
    }


    JSONObject jsonobj = null;
    String alerttime=null;
    String beforematch=null;
    JSONArray jArray = null;
    String starttime=null;
try{

    jsonobj = new JSONObject (response);
    jArray=jsonobj.getJSONArray("notifications");

    Log.i("Current Time",jArray+"");
    Log.i("Current Time",getCurrentTime());

    for(int i=0;i<jArray.length();i++){

        jsonobj=jArray.getJSONObject(i);

        alerttime=jsonobj.getString("alert");
        starttime=jsonobj.getString("startTime");
        beforematch=jsonobj.getString("beforeMatchTime");

        if(alerttime==getCurrentTime()){

                Notification(starttime);
                Log.i("Wintal", "wint");
        }
    }
    //teamStatus=jsonobj.getString("teamStatus");

}
catch(JSONException e){
        Log.e("log_tag", "Error parsing data "+e.toString());
}



Log.i("RESULT", jsonobj+"");
Log.i("time", alerttime+"");
Log.i("before Match",beforematch+"");
}

这是检查。

public void Notification(String s)
{

        String ns = Context.NOTIFICATION_SERVICE;

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.notification_icon;
        CharSequence tickerText = "Ready for Play time."; // ticker-text
        long when = System.currentTimeMillis();
        Context context = getApplicationContext();
        CharSequence contentTitle = "Play Time";
        CharSequence contentText = "Your match is at "+s;
        Intent notificationIntent = new Intent(this,ScheldueNotification .class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        Notification notification = new Notification(icon, tickerText, when);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        // and this
        final int HELLO_ID = 1;
        mNotificationManager.notify(HELLO_ID, notification);

        notification.flags=Notification.FLAG_AUTO_CANCEL;
}

这是通知部分。 请帮忙。


使用 AlarmManger 以特定时间间隔发出通知。

检查一下:

报警管理器描述 http://developer.android.com/reference/android/app/AlarmManager.html and 报警管理器示例 http://www.techrepublic.com/blog/software-engineer/use-androids-alarmmanager-to-schedule-an-event/

希望这可以帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

每当应用程序运行或不运行时显示通知 的相关文章

  • Laravel 登录后重定向回来

    登录后如何重定向返回页面 在 Laravel 5 2 中 认证控制器 protected redirectTo 重定向用户
  • 在我的Android中,当其他应用程序想要录制音频时如何停止录音?

    在我的应用程序中 服务通过 AudioRecord 持续录制音频 当我的应用程序运行时 其他与音频记录相关的应用程序 例如 Google 搜索 无法工作 如何知道何时有其他应用想要录制音频 以便我可以停止录制以释放资源 答案是MediaRe
  • 合并 2 个数组并合并数字键的结果

    我有 2 个数组 我希望通过每个数字键将其中合并 分组在一起 例如 Array1 2009 gt 131 2008 gt 940 2007 gt 176 2006 gt 1 Array2 2008 gt 9 2007 gt 3 我希望输出是
  • 更新到材质 1.2.0 后,材质按钮上缺少圆角半径属性

    这是我的材质按钮代码
  • php oracle客户端oci8安装出现什么问题

    我尝试了安装 PHP Oracle 客户端的所有过程 1 我安装了客户端版本8和32位 2 我在php ini中取消了oci的注释 3 重新启动Wamp 4 不确定是否真的安装 但我在 php ini 中得到了引用 5 但仍然无法连接 泰汉
  • 应用程序未安装在 Android 模拟器上

    我正在 android Geocoder 中开发一个应用程序 当我运行该应用程序时 它会显示 2011 01 11 11 08 13 GeoTourProject 自动目标模式 使用现有模拟器 emulator 5554 运行兼容的 AVD
  • 使用 yum 和 pear 安装 php-soap 均失败

    我正在尝试在 Centos 6 4 服务器上安装 PHP 的 SOAP 扩展 我对包管理器 从 CLI 安装包并在 PHP 中配置它们相当不熟悉 我相当有能力管理 php ini 和其他 PHP 配置文件 soap ini 等 我尝试使用以
  • Android 启动器快捷方式

    我制作了一个简单的打卡 打卡时钟应用程序 我想向用户添加在主屏幕上创建快捷方式的选项 该快捷方式将切换应用程序的状态 超时 超时 但我根本不希望此快捷方式在屏幕上打开应用程序 这是我的 setupShortcut private void
  • 从 android 简单上传到 S3

    我在网上搜索了从 android 上传简单文件到 s3 的方法 但找不到任何有效的方法 我认为这是因为缺乏具体步骤 1 https mobile awsblog com post Tx1V588RKX5XPQB TransferManage
  • php date_parse("2010 年 2 月") 给出日期 == 1

    当没有日期时 我将其称为 date parse 中的错误 d date parse Feb 2010 会给 d day 1 请参阅对此的评论date parse 手册页 http php net manual en function dat
  • PHP LDAP 查询获取特定安全组的成员

    我正在努力让 LDAP 查询工作来为我提供安全组的成员 我们的活动目录结构设置为 DC domain DC co dc uk然后 我们有一个名为 公司用户 的 OU 其中有一个用于 IT 和标准的 OU 在这些中我们创建了用户 所以我被设置
  • 设置大型电子邮件通知系统有哪些方法?

    我的公司有一个用 PHP 构建的网站 我们使用内置的 PHP 电子邮件功能每天向订阅者发送数千封电子邮件 这是一个糟糕的主意 它堵塞了我们的服务器 并且需要几个小时才能完成整个批次 现在我已经研究过像 MailChimp 这样的群发邮件服务
  • 如何在不更改手机语言的情况下更改Android应用程序语言?

    我希望用户在应用程序内选择一种语言 选择语言后 我希望字符串使用特定语言 如果我更改手机语言 那么我的应用程序将以设置的语言运行 我无法找到任何在不更改手机语言的情况下设置语言的方法 此外 一旦设置了语言 更改就应该反映出来 有人可以建议一
  • 将 JSON 参数从 java 发布到 sinatra 服务

    我有一个 Android 应用程序发布到我的 sinatra 服务 早些时候 我无法读取 sinatra 服务上的参数 但是 在我将内容类型设置为 x www form urlencoded 之后 我能够看到参数 但不完全是我想要的 我在
  • 使用 Espresso 检查 EditText 的字体大小、高度和宽度

    如何使用 Espresso 检查 EditText 的字体大小 高度和宽度 目前要分割我使用的文本 onView withId R id editText1 perform clearText typeText Amr 并阅读文本 onVi
  • if/else 简写来定义变量

    我很难理解 if else 的 php 简写是如何描述的here https stackoverflow com questions 20233207 php if shorthand and echo in one line possib
  • 在webview android中加载本地html文件

    我正在尝试在 android 的 webview 中加载 html 文件的内容 但是 它给了我 网页不可用错误 如果我尝试使用谷歌或雅虎等网站 它们就会起作用 html文件位于src gt main gt assests gt index
  • 如何将图像从 Android 应用程序上传到网络服务器的特定文件夹中

    如何将图像从 android 移动到 Web 服务器上的指定文件夹 这是我的安卓代码 package com example bitmaptest import java io ByteArrayOutputStream import ja
  • 通过 Sparkpost 发送 iCal 邀请

    我正在尝试使用 SparkPost 通过电子邮件以附件形式发送日历邀请 但收到电子邮件后邀请不会打开 我使用两个文件 calendarinvite php 来创建邀请 使用 Sendemail php 来发送电子邮件 calendarinv
  • 在 Google 地图上绘制线条/路径

    我很长一段时间都在忙于寻找如何在 HelloMapView 中的地图上的两个 GPS 点之间画一条线 但没有运气 谁能告诉我该怎么做 假设我使用扩展 MapView 的 HelloMapView 我需要使用叠加层吗 如果是这样 我是否必须重

随机推荐