Minggu, 15 April 2018

Thread Menu Utama Ke Database

Perintah sbb adalah perintah secara periode mengecek database server..jika ada data baru maaka sistem akan memebrikan sinyal notofikasi,,,





package com.example.lp2maray;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.notification_android.Login.ceklogin;
import com.example.notification_android.eventList.load;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MenuUtama extends Activity {
String ip="",kategori="",PESAN="";
JSONParser jParser = new JSONParser();
JSONArray myJSON = null;
int adabaru=0;
String kode_admin="";
String username="";
Handler mHandler;
private static final String TAG_SUKSES = "sukses";
private static final String TAG_record = "record";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menuutama);
    ip=jParser.getIP();
   
        this.mHandler = new Handler();
        m_Runnable.run();
     

//Sistem Pengujian ttg siapakah yg sedang login....
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(MenuUtama.this);
        Boolean Registered = sharedPref.getBoolean("Registered", false);
              if (!Registered){
                  finish();
              }else {
               kode_admin= sharedPref.getString("kode_admin", "");
               username = sharedPref.getString("username", "");
              
              }
           
           
        System.out.print("kode admin   "+kode_admin);
 
        ImageView      history=(ImageView)findViewById(R.id.history);
        history.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
          Intent i = new Intent(MenuUtama.this, Kategori.class);
                startActivity(i);
    }});
     
        ImageView      warning=(ImageView)findViewById(R.id.warning);
        warning.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
          Intent i = new Intent(MenuUtama.this, eventListWaning.class);
                startActivity(i);
    }});
     
        ImageView      about=(ImageView)findViewById(R.id.about);
        about.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
          Intent i = new Intent(MenuUtama.this, About.class);
                startActivity(i);
    }});
     
ImageView      admin=(ImageView)findViewById(R.id.setting);
    admin.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
      Intent i = new Intent(MenuUtama.this, admin.class);
            startActivity(i);
}});
 
    }
 
    private final Runnable m_Runnable = new Runnable()    {
        public void run()  {
            Toast.makeText(MenuUtama.this,"cek in runnable",Toast.LENGTH_SHORT).show();
           // addNotification();
      new load().execute();
            MenuUtama.this.mHandler.postDelayed(m_Runnable,5000);            
        }
    };
    
    private void addNotification() {
Toast.makeText(MenuUtama.this,"Info "+PESAN,Toast.LENGTH_SHORT).show();
    NotificationCompat.Builder builder =
          new NotificationCompat.Builder(this)
          .setSmallIcon(R.drawable.logo_kecil)
          .setContentTitle("WARNING, Ada Serangan ! ")
          .setContentText(PESAN);

       Intent notificationIntent = new Intent(this, eventList.class);
       notificationIntent.putExtra("kategori", kategori);
       
       PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
       builder.setContentIntent(contentIntent);
       NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
       manager.notify(0, builder.build());

    }  
    
    
    
    class load extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(ip+"cek.php", "GET", params);
Log.d("show: ", json.toString());
try {
// Log.v("BACA", "CEK DATA0.....BARU");
adabaru= json.getInt("sukses");
if(adabaru==1){
JSONArray myObj = json.getJSONArray(TAG_record); // JSON Array
final JSONObject myJSON = myObj.getJSONObject(0);
runOnUiThread(new Runnable() {
public void run() {
try {
PESAN=myJSON.getString("PESAN");
kategori="TCP";
if(PESAN.indexOf("UDP")>=0){kategori="UDP";}
else if(PESAN.indexOf("ICMP")>=0){kategori="ICMP";}

catch (JSONException e) {e.printStackTrace();}
}});

}


catch (JSONException e) {e.printStackTrace();}
return null;
}

protected void onPostExecute(String file_url) {
runOnUiThread(new Runnable() {
public void run() {
//Log.v("BACA0", "CEK DATA.....BARU");
if (adabaru==1){
adabaru=0;
//Log.v("BACA", "ADA DATA.....BARU:"+PESAN);
addNotification();
//Toast.makeText(MenuUtama.this,"ADAAAAAAAAAAAA DATA",Toast.LENGTH_SHORT).show();
}

}
});}
}
    
 
}

//=========
pada bagian manifet:

  <uses-permission android:name="android.permission.INTERNET" />

 <activity
            android:name="com.example.notification_android.Notification"
            android:label="Details of notification"
          android:parentActivityName=".MainActivity">
        <meta-data
        android:name="android.support.PARENT_ACTIVITY"
          android:value=".MainActivity"/>
        </activity>


NB: buatkan juga suatu halaman baru bernama Notification.java sebagai halaman target....saat notfi dibuka

Tidak ada komentar:

Posting Komentar