So I have this code
public class notif extends Activity {
private static final int NOTIFY_ME_ID=1337;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final NotificationManager mgr=(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification note=new Notification(R.drawable.icon,"New Problem ",System.currentTimeMillis());
note.flags|=Notification.FLAG_AUTO_CANCEL;
PendingIntent i=PendingIntent.getActivity(this, 0,new Intent(this,list.class), 0);
note.setLatestEventInfo(this,"Problem Occured","Click to see the problem",i);
mgr.notify(NOTIFY_ME_ID,note);
}
This code is about making notifications but i want it to just appear on my notification on top an not opening any page at first so it's like the notification can appear in the main menu without opening any page at first like.the sms notification that appear on top only without having to see the entire sms until we click the notification so can someone help me with this? :)
Create a class NotificationAlert and paste the code below there:
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
public class NotificationAlert extends Activity {
private static final int NOTIFY_ME_ID=1337;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_alert);
/*********** Create notification ***********/
final NotificationManager mgr=
(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification note=new Notification(R.drawable.stat_notify_chat,
"Android Example Status message!",
System.currentTimeMillis());
// This pending intent will open after notification click
PendingIntent i=PendingIntent.getActivity(this, 0,
new Intent(this, NotifyMessage.class),
0);
note.setLatestEventInfo(this, "Android Example Notification Title",
"This is the android example notification message", i);
//After uncomment this line you will see number of notification arrived
//note.number=2;
mgr.notify(NOTIFY_ME_ID, note);
}
}
Create a class NotifyMessage and paste the code below there:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class NotifyMessage extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView txt=new TextView(this);
txt.setText("Activity after click on notification");
setContentView(txt);
}
}
I think I know now that i have to use service and this is the code for making that service hope it helps other
public class servicenotif extends Service {
private static final String TAG = "servicenotif";
private static final int NOTIFY_ME_ID=1337;
private String url="Your URL";
SharedPreferences mPrefs;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
//Untuk bikin alarm yang ngecek ke database setiap 30 detik
Log.d(TAG, "onCreate");
Intent i = new Intent(this, servicenotif.class);
PendingIntent pintent = PendingIntent.getService(this, 0, i,0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 30*1000, pintent);
}
@Override
public void onStart(Intent intent, int startId) {
//buat preference
new LoginAsyncTask().execute();
//Note: You can start a new thread and use it for long background processing from here.
}
class LoginAsyncTask extends AsyncTask<String, Integer, String> {
String response = "";
ProgressDialog dialog;
//step2
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
//publishProgress(190);
mPrefs = getSharedPreferences("Preference", Context.MODE_PRIVATE);
//ngambil data preference
String txtTransID=mPrefs.getString("tid", "");
if(txtTransID.equals("")){
txtTransID="0";
}
int t=Integer.valueOf(txtTransID);
//membuat notifikasi yang muncul apabila ada problem dan hanya muncul lagi apabila ada problem baru
JSONObject json = JSONfunctions.getJSONfromURL("Your URL");
JSONObject js = JSONfunctions.getJSONfromURL("Your URL");
JSONObject j = JSONfunctions.getJSONfromURL("Your URL");
try{
JSONArray makanan = json.getJSONArray("makanan");
for(int i=0;i<makanan.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject jsonobj = makanan.getJSONObject(i);
map.put("id",jsonobj.getString("Status"));
//mengecek status yang berubah dari nol menjadi 1
if(jsonobj.getString("Status").equals("1")){
JSONArray aku=js.getJSONArray("makanan");
for(int a=0;a<aku.length();a++)
{
HashMap<String, String> mp = new HashMap<String, String>();
JSONObject jsb = aku.getJSONObject(a);
mp.put("take", jsb.getString("Taken"));
//mengecek Taken yang belum diambil
if(jsb.getString("Taken").equals("null")){
JSONArray tid=j.getJSONArray("makanan");
for(int Ti=0;Ti<tid.length();Ti++){
HashMap<String,String> trans=new HashMap<String, String>();
JSONObject jo=tid.getJSONObject(Ti);
trans.put("trans", jo.getString("Trans_ID"));
int TID;
//mengecek Trans_ID yang ditambahkan apa ada yang baru atau tidak
if(jo.getString("Trans_ID").equals("")){
TID=0;
}
else{
TID=Integer.valueOf(jo.getString("Trans_ID"));
}
if(t<TID){
Editor editor = mPrefs.edit();
editor.putString("tid", jo.getString("Trans_ID"));
editor.commit();
map.put("nama", jsonobj.getString("Andon_Type"));
map.put("harga", jsonobj.getString("Line"));
String andon=jsonobj.getString("Andon_Type");
String line=jsonobj.getString("Line");
NotificationManager mgr=(NotificationManager)servicenotif.this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification note=new Notification(R.drawable.icon,"New Problem ",System.currentTimeMillis());
note.flags|=Notification.FLAG_AUTO_CANCEL;
PendingIntent b=PendingIntent.getActivity(servicenotif.this, 0,new Intent(servicenotif.this,list2.class),PendingIntent.FLAG_ONE_SHOT);
note.setLatestEventInfo(servicenotif.this,"Problem Occured","Click to see the list of problem",b);
note.defaults=Notification.DEFAULT_ALL;
mgr.notify(NOTIFY_ME_ID,note);
}
//membuat notifikasi muncul lagi apabila data di reset
else if(t>TID){
Editor editor = mPrefs.edit();
editor.putString("tid", "");
editor.commit();
}
}
}
}
}
}
}
catch(JSONException e){
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
publishProgress(190);
return null;
}
//last step
@Override
protected void onPostExecute(String result) {
//dialog.cancel();
}
private void onCreate(Object object) {
// TODO Auto-generated method stub
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
// biasanya untuk progress download
int nilai = values[0];
// didapat dari publish progress
}
//step1
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
// buat bikin dialog loading
/*dialog = ProgressDialog.show(list.this, "Loading",
"Please wait..");*/
super.onPreExecute();
}
public void write(String fileName){
BufferedWriter bw=null;
try{
bw=new BufferedWriter(new OutputStreamWriter(openFileOutput(fileName,Context.MODE_PRIVATE)));
}
catch(Exception e){
}
}
}
private void getRequest(String Url2) {
// TODO Auto-generated method stub
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try {
HttpResponse response = client.execute(request);
} catch (Exception ex) {
Toast.makeText(this, "Penghandelan Gagal !", Toast.LENGTH_SHORT)
.show();
}
}
private String request(HttpResponse response) {
// TODO Auto-generated method stub
String result = "";
try {
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
str.append(line + "\n");
}
in.close();
result = str.toString();
} catch (Exception ex) {
result = "Error";
}
return result;
}
@Override
//untuk logout mematikan notifikasi dan alarm supaya tidak menjalankan service lagi
public void onDestroy() {
NotificationManager mgr=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mgr.cancelAll();
Toast.makeText(this, "Log out successfull", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
PendingIntent i=PendingIntent.getService(this, 0,new Intent(this,servicenotif.class), 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.cancel(i);
}
Hope this will help
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.