MainPage.java
public class MainPage extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
insert = (Button) findViewById(R.id.btnInsert);
lv = (ListView) findViewById(R.id.listView1);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
for (int j = 0; j < adapterView.getChildCount(); j++)
adapterView.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);
view.setBackgroundColor(Color.BLUE);
l+=1;
selectedItem = lv.getItemAtPosition(i).toString();
String[] separated = selectedItem.split("-");
selectedItemId = separated[0];
selectedItem = separated[1].trim();
new UpdateDelete().show(getFragmentManager(), DISPLAY_SERVICE);
}
});
insert.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
type = "insert";
tableName = "person";
new UpdateInsert().show(getFragmentManager(), DISPLAY_SERVICE);
}
});
type="select";
tableName="person";
new HttpAsyncTask().execute();
}
public class UpdateDelete extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("")
.setPositiveButton("Update", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
type= "update";
tableName= "person";
new UpdateInsert().show(getFragmentManager(), DISPLAY_SERVICE);
}
})
.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
type= "delete";
tableName= "person";
pid= selectedItemId;
new HttpAsyncTask().execute();
}
});
return builder.create();
}
}
public class UpdateInsert extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.screen_popup, null);
etId = (EditText) view.findViewById(R.id.pid);
etName = (EditText) view.findViewById(R.id.name);
if(type.equals("update")){
etId.setText(selectedItemId);
etName.setText(selectedItem);
etId.setKeyListener(null);
}
builder.setView(view)
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
pid = etId.getText().toString();
name = etName.getText().toString();
new HttpAsyncTask().execute();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
UpdateInsert.this.getDialog().cancel();
}
});
return builder.create();
}
}
public static void setGCM(){
type = "select";
tableName = "person";
MainPage.HttpAsyncTask hp = new MainPage().new HttpAsyncTask();
hp.execute();
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
public ArrayList<String> aList= new ArrayList<String>();
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("type", type));
params.add(new BasicNameValuePair("table_name", tableName));
if(type.equals("insert") || type.equals("update")){
params.add(new BasicNameValuePair("id", pid));
params.add(new BasicNameValuePair("name", name));
}else if(type.equals("delete")){
Log.d("DELETE:", pid);
params.add(new BasicNameValuePair("id", pid));
}
JSONObject json = jsonParser.makeHttpRequest(url, "POST", params);
Log.d("Create Response", json.toString());
if(type.equals("select")){
try {
jsonRows = json.getJSONArray("rows");
for(int i = 0; i<jsonRows.length();i++){
JSONObject p = (JSONObject)jsonRows.get(i);
aList.add(p.getString("ID") + " - " +p.getString("Name"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}else{
try{
String result = json.getString("results");
return result;
}catch(JSONException e){
e.printStackTrace();
}
}
return null;
}
protected void onPostExecute(String result) {
if(type.equals("select")){
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainPage.this, R.layout.custom_textview, aList);
lv.setAdapter(arrayAdapter);
}else{
if(result.equals("success")){
Toast.makeText(getBaseContext(), type + " successfully!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(), "Something went wrong!", Toast.LENGTH_SHORT).show();
}//else
}
type = "";
}
}
}
GcmMessageHandler.java
public class GcmMessageHandler extends IntentService {
String mes;
private Handler handler;
public GcmMessageHandler() {
super("GcmMessageHandler");
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
handler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
mes = extras.getString("title");
showToast();
Log.i("GCM", "Received : (" +messageType+") "+extras.getString("title"));
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
public void showToast(){
handler.post(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),mes , Toast.LENGTH_LONG).show();
MainPage.setGCM();
}
});
}
}
ERRORS :
09-22 07:16:21.718: E/AndroidRuntime(2830): FATAL EXCEPTION: main 09-22 07:16:21.718: E/AndroidRuntime(2830): java.lang.IllegalStateException: System services not available to Activities before onCreate() 09-22 07:16:21.718: E/AndroidRuntime(2830): at android.app.Activity.getSystemService(Activity.java:4492) 09-22 07:16:21.718: E/AndroidRuntime(2830): at android.widget.ArrayAdapter.init(ArrayAdapter.java:310) 09-22 07:16:21.718: E/AndroidRuntime(2830): at android.widget.ArrayAdapter.(ArrayAdapter.java:153) 09-22 07:16:21.718: E/AndroidRuntime(2830): at com.argenit.monitor.MainPage$HttpAsyncTask.onPostExecute(MainPage.java:174) 09-22 07:16:21.718: E/AndroidRuntime(2830): at com.argenit.monitor.MainPage$HttpAsyncTask.onPostExecute(MainPage.java:1) 09-22 07:16:21.718: E/AndroidRuntime(2830): at android.os.AsyncTask.finish(AsyncTask.java:631) 09-22 07:16:21.718: E/AndroidRuntime(2830): at android.os.AsyncTask.access$600(AsyncTask.java:177) 09-22 07:16:21.718: E/AndroidRuntime(2830): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644) 09-22 07:16:21.718: E/AndroidRuntime(2830): at android.os.Handler.dispatchMessage(Handler.java:99) 09-22 07:16:21.718: E/AndroidRuntime(2830): at android.os.Looper.loop(Looper.java:137) 09-22 07:16:21.718: E/AndroidRuntime(2830): at android.app.ActivityThread.main(ActivityThread.java:5103) 09-22 07:16:21.718: E/AndroidRuntime(2830): at java.lang.reflect.Method.invokeNative(Native Method) 09-22 07:16:21.718: E/AndroidRuntime(2830): at java.lang.reflect.Method.invoke(Method.java:525) 09-22 07:16:21.718: E/AndroidRuntime(2830): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 09-22 07:16:21.718: E/AndroidRuntime(2830): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 09-22 07:16:21.718: E/AndroidRuntime(2830): at dalvik.system.NativeStart.main(Native Method)
GCM handling the message then i want to execute HttpAsyncTask. I couldnt access HttpAsyncTask from GcmMessageHandler then i create a function which is static setGCM(). When the message come to phone, i call this method then try to execute httpasynctask. but i got some errors. where is my mistake?
0 comments:
Post a Comment