每当应用程序正在运行或不显示通知 [英] Display notification whenever app is running or not

查看:162
本文介绍了每当应用程序正在运行或不显示通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序,通知必须在每次应用程序正在运行或没有激活。我应该把我的通知方法中的onCreate()?我的通知就像报警。请检查小有一点。

In my program, notification must be activated whenever app is running or not. Should I put my notification method in onCreate()? my notification is just like alarm. Please check alittle.

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;
}

这是通知的一部分。 请大家帮帮忙。

This is Notification part. Please help.

推荐答案

使用AlarmManager与BroadcastReceiver的。 使用报警管理器触发接收器和接收器类中有你的通知方法。 BroadcastReceiver的让你无论是应用程序正在运行或不运行后台进程。

Use AlarmManager with BroadcastReceiver. Using alarm manager trigger the receiver and inside your receiver class have your notification method. BroadcastReceiver allows you to run processes in background whether is app is running or not.

这篇关于每当应用程序正在运行或不显示通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆