Android 从webservice获取数据后是否可以创建通知?
我怀疑我是否可以在从webservice获取数据后创建通知需要通过通知管理器创建通知是否可能,因为我看不到任何关于此的工作模型我不喜欢在GCM或其他推送通知中使用我需要使用通知管理器创建通知是否可能让我更准确地解释我的问题,当我从webservice获取新数据到sqlite时,我需要获得通知,如果没有推送通知方法,我如何实现这一点 这是我在活动中从服务器获取记录并将其保存在sqlite中的webservice代码:Android 从webservice获取数据后是否可以创建通知?,android,notifications,Android,Notifications,我怀疑我是否可以在从webservice获取数据后创建通知需要通过通知管理器创建通知是否可能,因为我看不到任何关于此的工作模型我不喜欢在GCM或其他推送通知中使用我需要使用通知管理器创建通知是否可能让我更准确地解释我的问题,当我从webservice获取新数据到sqlite时,我需要获得通知,如果没有推送通知方法,我如何实现这一点 这是我在活动中从服务器获取记录并将其保存在sqlite中的webservice代码: public class AccountLiast extends
public class AccountLiast extends AsyncTask<String, Void, Void> {
ProgressDialog Dialog = new ProgressDialog(Accounts.this);
@Override
protected void onPreExecute() {
super.onPreExecute();
Dialog.setMessage("Syncing Please Wait");
Dialog.show();
}
@Override
protected Void doInBackground(String... params) {
RequestQueue queue = Volley.newRequestQueue(getBaseContext());
JsonObjectRequest jsonObjRequest = new JsonObjectRequest(Request.Method.GET, params[0], new JSONObject(),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
String server_response = response.toString();
try {
Model_Account modelobjs = new Model_Account();
JSONObject json_object = new JSONObject(server_response);
JSONArray json_array = new JSONArray(json_object.getString("AccountPageLoadAccountListResult"));for (int i = 0; i < json_array.length(); i++) {
JSONObject json_arrayJSONObject = json_array.getJSONObject(i);
modelobjs.setCompany_group(json_arrayJSONObject.getString("CompanyGroup"));
modelobjs.setParent_company(json_arrayJSONObject.getString("CompanyName"));
modelobjs.setState(json_arrayJSONObject.getString("Region"));
modelobjs.setAccountID(json_arrayJSONObject.getInt("AccountID"));
modelobjs.setCompany_name(json_arrayJSONObject.getString("CompanyName"));
//This is where i saving it in sqlite db
account_sf_db.InsertorUpdate(modelobjs);
accountListAdapter.addModelClass(modelobjs);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getBaseContext(), error.toString(), Toast.LENGTH_SHORT).show();
}
});
queue.add(jsonObjRequest);
return null;
}
@Override
protected void onPostExecute(Void modleobjs) {
super.onPostExecute(modleobjs);
Dialog.hide();
}
公共类AccountLiast扩展异步任务{
ProgressDialog=新建ProgressDialog(Accounts.this);
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
setMessage(“正在同步,请稍候”);
Dialog.show();
}
@凌驾
受保护的Void doInBackground(字符串…参数){
RequestQueue=Volley.newRequestQueue(getBaseContext());
JsonObjectRequest JsonObjectRequest=新的JsonObjectRequest(Request.Method.GET,参数[0],新的JSONObject(),
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
字符串服务器_response=response.toString();
试一试{
Model_Account modelobjs=新的Model_Account();
JSONObject json_object=新的JSONObject(服务器_响应);
JSONArray json_数组=新JSONArray(json_object.getString(“AccountPageLoadAccountListResult”);for(int i=0;i
- 您可以使用内置的通知功能
public void generateNotification(String alertMessage) {
Intent offlineIntent = new Intent(this, NotificationReceiverActivity.class);
offlineIntent.setAction("Go Offline");
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.logo);
long when = System.currentTimeMillis();
Uri alarmSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent notificationIntent = new Intent(getApplicationContext(), ActivityHome.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent mainPIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
Notification noti = new Notification.Builder(this)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(alertMessage)
.setSmallIcon(R.mipmap.logo)
.setLargeIcon(largeIcon)
.setAutoCancel(true)
.setSound(alarmSound).setAutoCancel(true).setWhen(when)
.setContentIntent(mainPIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(m, noti);
}
--这里我在方法中传递json响应alertMessage.:
generateNotification(String alertMessage)
下面的代码片段演示了一个简单的通知,指定用户单击该通知时要打开的活动。请注意,该代码创建了一个
TaskStackBuilder
对象,并使用该对象为该操作创建PendingEvent
。此模式在“保留导航”一节中有更详细的说明开始活动时:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
//Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
这是一个创建简单通知的示例,将此代码段放在需要显示的地方并进行自定义
阅读有关通知的更多信息:是的,您可以在从Web服务获取数据后创建通知 我假设您是在
服务
上获取此数据的,因此下面您可以找到一个在服务
上获取数据的小示例
编辑我根据您的需要更改了您的doInBackground
@Override
protected Void doInBackground(String... params) {
RequestQueue queue = Volley.newRequestQueue(getBaseContext());
JsonObjectRequest jsonObjRequest = new JsonObjectRequest(Request.Method.GET, params[0], new JSONObject(),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
String server_response = response.toString();
try {
Model_Account modelobjs = new Model_Account();
JSONObject json_object = new JSONObject(server_response);
JSONArray json_array = new JSONArray(json_object.getString("AccountPageLoadAccountListResult"));for (int i = 0; i < json_array.length(); i++) {
JSONObject json_arrayJSONObject = json_array.getJSONObject(i);
modelobjs.setCompany_group(json_arrayJSONObject.getString("CompanyGroup"));
modelobjs.setParent_company(json_arrayJSONObject.getString("CompanyName"));
modelobjs.setState(json_arrayJSONObject.getString("Region"));
modelobjs.setAccountID(json_arrayJSONObject.getInt("AccountID"));
modelobjs.setCompany_name(json_arrayJSONObject.getString("CompanyName"));
//This is where i saving it in sqlite db
account_sf_db.InsertorUpdate(modelobjs);
showNotification(modelobjs); // This is where you will show your notification
accountListAdapter.addModelClass(modelobjs);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getBaseContext(), error.toString(), Toast.LENGTH_SHORT).show();
}
});
queue.add(jsonObjRequest);
return null;
}
private void showNotification (Model_Account modelobjs) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(modelobjs.getCompany_name());
.setContentText("This is notification message");
Intent resultIntent = new Intent(this, MainActivity.class); //put your activity here
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());
}
@覆盖
受保护的Void doInBackground(字符串…参数){
RequestQueue=Volley.newRequestQueue(getBaseContext());
JsonObjectRequest JsonObjectRequest=新的JsonObjectRequest(Request.Method.GET,参数[0],新的JSONObject(),
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
字符串服务器_response=response.toString();
试一试{
Model_Account modelobjs=新的Model_Account();
JSONObject json_object=新的JSONObject(服务器_响应);
JSONArray json_数组=新JSONArray(json_object.getString(“AccountPageLoadAccountListResult”);for(int i=0;i