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

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(使用前将#替换为@)

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

随机推荐