我想在我的android应用程序中做一个简单的通知,但它总是会出现裂缝。 我在做什么是我在main activity
点击时有一个按钮,它会移动以下通知类: package com.megasoft.entangle;Android简单通知
import java.util.concurrent.ExecutionException;
import org.json.JSONException;
import org.json.JSONObject;
import com.megasoft.requests.GetRequest;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.widget.TextView;
public class OfferNotification extends Activity {
String notification = "";
String offerer = "";
String description = "";
String request = "";
String amount = "";
String notificationID = "1";
final String link = "Click here to view";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.offer_notification);
GetRequest getRequest = new GetRequest("http://entangle2.apiary.io/notification/1");
getRequest.addHeader("sessionID" , "testest");
getRequest.execute("http://entangle2.apiary.io/notification/1");
try {
JSONObject json = new JSONObject(getRequest.get());
description = json.getString("description");
offerer = json.getString("offerer");
request = json.getString("request");
amount = json.getString("Amount");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
notification = offerer + " has offered " + amount + " on your " + request + link;
TextView txt = (TextView) findViewById(R.id.textView1);
txt.setText(notification);
createNotification();
}
public void createNotification() {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(RESULT_OK)
.setContentTitle("Entangle:You got a new offer!")
.setContentText(notification);
Intent resultIntent = new Intent(this , Notification.class);
TaskStackBuilder sb = TaskStackBuilder.create(this);
sb.addParentStack(Notification.class);
sb.addNextIntent(resultIntent);
PendingIntent pIntent =
sb.getPendingIntent(0 , PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pIntent);
NotificationManager notificationMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
int ID = Integer.parseInt(notificationID);
notificationMgr.notify(ID , mBuilder.build());
}
}
谢谢我做了所有这三个变化,并没有裂缝了,但仍然没有创建通知。 – user3100088
检查设备设置,如果显示通知已启用您的应用程序 – Libin
通知已启用,我认为这是因为我在应用程序 – user3100088