Android在服务方法中显示通知
感谢您阅读我的问题。Android在服务方法中显示通知,android,notifications,Android,Notifications,感谢您阅读我的问题。 我编写了一个简单的应用程序,向运行后台的服务显示通知: 我将其写入服务类以显示简单通知: NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getBaseContext()) .setSmallIcon(R.drawable.maleki) // notification icon .se
我编写了一个简单的应用程序,向运行后台的服务显示通知:
我将其写入服务类以显示简单通知:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getBaseContext())
.setSmallIcon(R.drawable.maleki) // notification icon
.setContentTitle("آذر گشت") // title for notification
.setContentText("شما"+" "+jsonArray.length()+" "+"خبر جدید دارید") // message for notification
.setAutoCancel(true); // clear notification after click
但不要给我看通知,会发生什么事?谢谢 我的完整服务类别代码为:
public class newsservice extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// your code
new HttpAsyncTask().execute("http://tour.maxitem.org/newslenght.aspx");
//this.stopSelf();
return Service.START_FLAG_REDELIVERY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
//************************************************************
//***************************************AsyncTask
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
TourMyCountry country=new TourMyCountry();
country.id="jamalnews";//.setName(etName.getText().toString());
return POST(urls[0],country);
}
// onPostExecute displays the results of the AsyncTask.
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
try {
JSONArray jsonArray=new JSONArray(result);
/*for(int i=0;i<jsonArray.length();i++)
{
JSONObject JsonObj=jsonArray.getJSONObject(i);
String countryname=JsonObj.getString("countryname");
}*/
/*NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getBaseContext())
.setSmallIcon(R.drawable.maleki) // notification icon
.setContentTitle("آذر گشت") // title for notification
.setContentText("شما"+" "+jsonArray.length()+" "+"خبر جدید دارید") // message for notification
.setAutoCancel(true); // clear notification after click*/
} catch (JSONException e) {
e.printStackTrace();
}
}
}//END OF ASYNCTASK
//**************************************POST LEVEL
public static String POST(String url, TourMyCountry myCountry){
InputStream inputStream = null;
String result = "";
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("id", myCountry.id);
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// 10. convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
Log.d("behzad in the service:",result);
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
// 11. return result
return result;
}//END OF POST BACK TO RETURN
//************************************************convertInputStreamToString
private static String convertInputStreamToString(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
//***************************************************************
public class TourMyCountry{
String id;
}
}
公共类新闻服务扩展服务{
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
//你的代码
新建HttpAsyncTask()。执行(“http://tour.maxitem.org/newslenght.aspx");
//这个。stopSelf();
退货服务。开始\标志\重新交付;
}
@凌驾
公共IBinder onBind(意向){
返回null;
}
//************************************************************
//***************************************异步任务
私有类HttpAsyncTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…URL){
TourMyCountry国家=新TourMyCountry();
country.id=“jamalnews”//.setName(etName.getText().toString());
回帖(URL[0],国家);
}
//onPostExecute显示异步任务的结果。
//onPostExecute显示异步任务的结果。
@凌驾
受保护的void onPostExecute(字符串结果){
试一试{
JSONArray JSONArray=新JSONArray(结果);
/*对于(int i=0;i试试这个
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// your code for notification builder
mNotifyMgr.notify(1, mBuilder.build());
我看不到实际设置通知的方法。它应该如下所示:
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
查看此链接,该链接描述了如何发送通知:
总而言之,您的代码不完整。您包含的源代码不够。请共享更多信息,例如您必须实际显示通知的呼叫。请包含更多代码,或者我建议在没有足够信息的情况下关闭此呼叫。@Booger I update questionyou have call notify()方法,它在哪里?@davidjons我怎么能称之为?为什么您的服务类中有一个AsyncTask?!这已经脱离UI线程,所以您不需要或不想要它。