我在我的应用中使用通知来警告用户是否确认了他的出席情况。但是在确认自数据库更改后哪个有效后,通知不会消失。而且我不知道这是否有连接,但我的屏幕会被重定向回到通知发布者活动的空白屏幕(因为我不会在该活动中宣布任何视图,因为它只是暂时的访问) 这是我的通知发布者的类代码:
package travenz.tacos;
import android.app.Activity;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
public class JobNotif extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, ScheduleList.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Intent intent2 = new Intent(this, ConfirmJobAttendance.class);
intent2.putExtra("Tugas_ID", getIntent().getExtras().getString("Tugas_ID"));
intent2.putExtra("Anggota_ID", getIntent().getExtras().getString("Anggota_ID"));
PendingIntent pIntent2 = PendingIntent.getActivity(this, 0, intent2, 0);
Intent intent3 = new Intent(this, RefuseJobAttendance.class);
intent3.putExtra("Tugas_ID", getIntent().getExtras().getString("Tugas_ID"));
intent3.putExtra("Anggota_ID", getIntent().getExtras().getString("Anggota_ID"));
PendingIntent pIntent3 = PendingIntent.getActivity(this, 0, intent3, 0);
// Build notification
// Actions are just fake
Notification noti = new Notification.Builder(this)
.setContentTitle("You Haven't Confirmed to Attend a Job")
.setContentText("Job Confirmation").setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.addAction(R.drawable.confirm, "Confirm", pIntent2)
.addAction(R.drawable.confused, "Not Now", pIntent)
.addAction(R.drawable.no, "Not Attending", pIntent3)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
}
这是我的通知接收者的课程:
package travenz.tacos;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;
public class ConfirmJobAttendance extends Activity {
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
// url to create new product
private static String UPDATE_JOB_ATTENDANCE_URL = "http://10.0.2.2/DatabaseCon/updateabsensitugas.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new UpdateJob().execute();
Intent i = new Intent(this,MainActivity.class);
startActivity(i);
}
public void onPause(){
super.onPause();
if(pDialog != null)
pDialog.dismiss();
}
/**
* Background Async Task to Create new product
* */
class UpdateJob extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ConfirmJobAttendance.this);
pDialog.setMessage("Confirming Attendance..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Tugas_ID", getIntent().getExtras().getString("Tugas_ID")));
params.add(new BasicNameValuePair("Anggota_ID", getIntent().getExtras().getString("Anggota_ID")));
params.add(new BasicNameValuePair("Absen", "1"));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.setJSONFromUrl(UPDATE_JOB_ATTENDANCE_URL, params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created job
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
Toast t = Toast.makeText(ConfirmJobAttendance.this, "You Have Updated Your Attendance", 1000);
t.show();
Exit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.confirm_job_attendance, menu);
return true;
}
public void Exit(){
Intent i = new Intent(this,MainActivity.class);
startActivity(i);
}
}
答案 0 :(得分:0)
如果您使用的是通知类,请执行以下操作:
Notification noti = new Notification.Builder(this);
noti.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
noti.setContentTitle("You Haven't Confirmed to Attend a Job")
.setContentText("Job Confirmation").setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.addAction(R.drawable.confirm, "Confirm", pIntent2)
.addAction(R.drawable.confused, "Not Now", pIntent)
.addAction(R.drawable.no, "Not Attending", pIntent3);
noti.build();
对于那些使用通知构建器的人:添加以下行:
notiBuilder.setAutoCancel(true);