Android : Java Android How to organize notification service

on Sunday, November 2, 2014


I'm noob in java and just started teaching. I want to organize service that connects to server and gets jsonObject from there. I also have class in my application that do the same thing. So can I connect from service to that class or I need to write new code separately in service?


Now I tried to do it in service, but there are some problems with JSON.


Here is my service:



public class NoticeService extends IntentService {
NotificationManager nm;
SharedPreferences sPref;

public NoticeService() {
super("appNotices");
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

}

@Override
public void onCreate() {
super.onCreate();
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Context context = getApplicationContext();
sPref = PreferenceManager.getDefaultSharedPreferences(context);
sPref = context.getSharedPreferences("token", MODE_PRIVATE);
}

public int onStartCommand(Intent intent, int flags, int startId) {
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
sendNotif();
stopSelf();
return START_STICKY;
}

void sendNotif() throws JSONException{

JSONObject res = checkNotices();

//here I'll send notices..
}

private JSONObject checkNotices() throws JSONException{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/api");
JSONObject res = null;

// Create a new HttpClient and Post Header
JSONObject json = new JSONObject();
JSONObject cont = new JSONObject();


cont.put("action", "/user/get_notices");
cont.put("token", sPref.getString("token",""));
json.put("request", cont);

Log.d("Requesting","json: " + json.toString());
Log.d("Requesting","Ran send");
try{
StringEntity se = new StringEntity(json.toString());
httppost.setEntity(se);
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpclient.execute(httppost);
Log.d("Requesting",httpResponse.toString());
// Execute HTTP Post Request
System.out.print(json);
HttpResponse response = httpclient.execute(httppost);

// for JSON:
if(response != null)
{
InputStream is = response.getEntity().getContent();

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String d = sb.toString();
Log.d("Requesting log",d);
JSONObject jObj = new JSONObject(d);
JSONObject resObj = jObj.getJSONObject("response");
res = resObj.getJSONObject("data");

}

}catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return res;
}

@Override
public void onDestroy() {
// I want to restart this service again in one hour
AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
alarm.set(
alarm.RTC_WAKEUP,
System.currentTimeMillis() + (1000 * 20 * 1),
PendingIntent.getService(this, 0, new Intent(this, NoticeService.class), 0)
);
}

public IBinder onBind(Intent arg0) {
return null;
}
}


So now android studio asks me to add "throws exception" to onStartCommand() but then I have error in log: Impossiblу to throws exceptions in onStartCommand().


What I have to do?


0 comments:

Post a Comment