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

时间:2013-12-05 06:55:32

标签: php android

在我的程序中,只要应用程序正在运行,就必须激活通知。我应该将我的通知方法放在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;
}

这是通知部分。 请帮忙。

2 个答案:

答案 0 :(得分:0)

使用AlarmManger以特定间隔通知。

检查一下:

Alaram Manager DescriptionAlarm Manager Example

希望这有帮助。

答案 1 :(得分:0)

将AlarmManager与BroadcastReceiver一起使用。 使用警报管理器触发接收器和接收器类内部有您的通知方法。 BroadcastReceiver允许您在后台运行进程,无论应用程序是否正在运行。