public class AndroidServiceStartOnBoot extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
showToast();
return START_REDELIVER_INTENT;
}
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
showToast();
}
};
private void showToast() {
logInRequest();
int icon = R.drawable.nicon;
long when = System.currentTimeMillis();
Intent intent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivities(AndroidServiceStartOnBoot.this, 0, new Intent[]{intent}, 0);
Notification notification = new Notification(icon, "Custom Notification", when);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
contentView.setImageViewResource(R.id.image, R.drawable.nicon);
contentView.setTextViewText(R.id.title, "Custom notification");
contentView.setTextViewText(R.id.text, status);
contentView.setOnClickPendingIntent(R.id.closeOnFlash, pendingIntent);
notification.contentView = contentView;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.flags = Notification.FLAG_AUTO_CANCEL; //Do not clear the notification
notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
notification.defaults |= Notification.DEFAULT_SOUND; // Sound
mNotificationManager.notify(0, notification);
new Handler().postDelayed(mRunnable, 4000);
}
private void logInRequest() {
SharedPreferences tprefs = getSharedPreferences("com.nahid.com.gsdambassadorpractice", MODE_PRIVATE);
String token = tprefs.getString("token", "false");
String url = Common.baseUrl + "ambassadorcall/api/call/addcard";
Map<String, String> param = new HashMap<String, String>();
param.put("token", token);
param.put("cardno", "125847");
JSON2Request json2Request = new JSON2Request(Request.Method.POST, url, param, logInListeneryy());
RequestManager.showProgressDlg(AndroidServiceStartOnBoot.this);
RequestManager.addRequest(json2Request, this);
}
private Response.Listener<JSONObject> logInListeneryy() {
return new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
RequestManager.dismissProgressDlg();
JSONObject jobj = response;
try {
if (jobj.getString("status").equals("success")) {
status = jobj.getString("status");
Toast.makeText(AndroidServiceStartOnBoot.this, "Working", Toast.LENGTH_LONG).show();
//String redirect =stepManage.gotostep(LoginActivity.this,jobj.getString("nextstep"));
/**
String token = tprefs.getString("token", "false");
Toast.makeText(LoginActivity.this, token, Toast.LENGTH_LONG).show();
**/
} else {
Toast.makeText(AndroidServiceStartOnBoot.this, jobj.getString("message"), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
}
使用服務類我想打一個服務,將通過使用通知管理器顯示自定義通知。但我想通過使用API從服務類。甚至,當應用程序更新通知是不是使用。我怎樣才能做到這一點在Android請幫助我..調用API和節目的通知在機器人
發表您的代碼到目前爲止您做了什麼 – Anjali
我已經發布了我的代碼。請立即使用loginrequest()函數我正在調用api並使用loginListneryy()我從server.i獲取結果想要顯示導致通知。 – Nahid