1

I have implemented an Alarm Manager in my WebviewActivity that is supposed to call a Service after every 15 minutes in the Dashboard Fragment. When I try run the application I get the following Logcat error.

Logcat

09-15 04:34:35.250  21867-21867/com.example.s210121629.myhealth E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.s210121629.myhealth, PID: 21867
    java.lang.RuntimeException: Unable to instantiate service com.example.s210121629.myhealth.dashboard.DashboardActivity$MyService: java.lang.InstantiationException: can't instantiate class com.example.s210121629.myhealth.dashboard.DashboardActivity$MyService; no empty constructor
            at android.app.ActivityThread.handleCreateService(ActivityThread.java:2786)
            at android.app.ActivityThread.access$1900(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
            at android.os.Handler.dispatchMessage(Handler.java:102)

WebviewActivity

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.oauth_activity);
        ButterKnife.inject(this);
        pref = getSharedPreferences("AppPref", MODE_PRIVATE);
        myself = this;

        //TODO: Alarmmanager
        _oauth_signin = (Button) findViewById(R.id.oauth_signin);
        _refresh = (Button) findViewById(R.id.refresh);
        BASE64Encoder encoder = new BASE64Encoder();
        concatinate = CLIENT_ID + ":" + CLIENT_SECRET;
        concate = encoder.encode(concatinate.getBytes());
        mdbhelper = new DBHelper(getApplicationContext());

        _oauth_signin.setOnClickListener(new View.OnClickListener() {
            Dialog auth_dialog;

            @Override
            public void onClick(View v) {


                //TODO : Pop-up dialog to be displayed with Oauth2.0
                auth_dialog = new Dialog(WebViewActivity.this);
                auth_dialog.setContentView(R.layout.webview);
                _webview = (WebView) auth_dialog.findViewById(R.id.webView);
                _webview.getSettings().setJavaScriptEnabled(true);
                _webview.loadUrl(OAUTH_URL + "?response_type=" + RESPONSE_TYPE + "&client_id=" + CLIENT_ID + "&redirect_uri=" + REDIRECT_URI + "&scope=" + SCOPE + "&expires_in=" + EXPIRES_IN);
                _webview.setWebViewClient(new WebViewClient() {

                    boolean authComplete = false;
                    Intent resultIntent = new Intent();

                    @Override
                    public void onPageStarted(WebView view, String url, Bitmap favicon) {
                        super.onPageStarted(view, url, favicon);
                    }


                    @Override
                    public void onPageFinished(WebView view, String url) {
                        super.onPageFinished(view, url);

                        if (url.contains("?code=") && authComplete != true) {
                            Uri uri = Uri.parse(url);
                            authCode = uri.getQueryParameter("code");
                            Log.i("", "CODE : " + authCode);
                            authComplete = true;
                            resultIntent.putExtra("code", authCode);
                            WebViewActivity.this.setResult(Activity.RESULT_OK, resultIntent);
                            setResult(Activity.RESULT_CANCELED, resultIntent);
                            SharedPreferences.Editor edit = pref.edit();
                            edit.putString("Code", authCode);
                            edit.commit();
                            auth_dialog.dismiss();
                            new TokenGet().execute();
                            Toast.makeText(getApplicationContext(), "Authorization Code is: " + authCode, Toast.LENGTH_SHORT).show();
                            //        new RequestAccess().execute();
                        } else if (url.contains("error=access_denied")) {
                            Log.i("", "ACCESS_DENIED_HERE");
                            resultIntent.putExtra("code", authCode);
                            authComplete = true;
                            setResult(Activity.RESULT_CANCELED, resultIntent);
                            Toast.makeText(getApplicationContext(), "Error Occurred", Toast.LENGTH_SHORT).show();
                            auth_dialog.dismiss();
                        }
                    }

                });

                auth_dialog.setTitle("Authorize myHealth");
                auth_dialog.setCancelable(true);
                auth_dialog.show();
            }
        });
        AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, DashboardActivity.MyService.class);
      //  intent.setAction(MyReceiver.ACTION_ALARM_RECEIVER);//my custom string action name
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);//used unique ID as 1001
        long start = System.currentTimeMillis() + TimeUnit.MINUTES.toMinutes(2);
        long next =  TimeUnit.MINUTES.toMinutes(5);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, start, AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);//first start will start

DashboardFragment

public class **MyService** extends Service{
    public MyService() {
        super();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
      new refresh().execute();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
private class refresh extends AsyncTask<String, String, JSONObject> {
    @Override
    protected JSONObject doInBackground(String... params) {
        RefreshToken jParser = new RefreshToken();
        JSONObject json = jParser.refreshToken(TOKEN_URL, concate + "=", GRANT_TYPE_REFRESH_TOKEN, refreshToken);
        return json;
    }

    @Override
    protected void onPostExecute(JSONObject jsonObject) {

        if (jsonObject != null) {

            try {

                newAccess = jsonObject.getString("access_token");
                newRefresh = jsonObject.getString("refresh_token");


                Log.d("Refresh", newRefresh);

                mdbhelper.updateToken(newAccess, newRefresh, concate);
                Toast.makeText(getActivity(), "Token Updated", Toast.LENGTH_SHORT).show();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } else {
            Toast.makeText(getActivity(), "Network Error", Toast.LENGTH_SHORT).show();

        }
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
}

Manifest

  <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.wake_lock" />
    <service
        android:name=".dashboard.DashboardActivity$MyService"
        android:enabled="true"></service>

The MyService class is an inner class in the DashboardActivity, because I want to gain access to network call methods in the DashboardFragment. The Myservice Class does have a constructor inside. If I make Myservice static I will not be able to access any of the methods of the outer class.

Tungamirai
  • 81
  • 11

2 Answers2

0

Your code is a little bit confused.

  1. in DashboardFragment, you actually declared a service.
  2. Your service class name is DashboardActivity.MyService.class, which is quiet questionable,because service should always isolated from activity.

So please try to rename DashboardFragment to Myservice, and generate intent like:

Intent intent = new Intent(this, MyService.class);

also, change your Manifest file to

<service
android:name="Your service pacakge + "." + MyService"
android:enabled="true">
</service>

For example, if your service's package is com.android.system. then your android name should be"

 com.android.system.MyService
SHICONG CAO
  • 219
  • 3
  • 7
  • The reason it is DashboardActivity.Myservice.class is because the class is an inner class of the DashboardFragment. – Tungamirai Sep 15 '15 at 05:57
0

If you want put your Service to be an inner class.The service must be a static class.Like that:

public static class MyService extends Service {
    public MyService() {
        super();
    }}
Andy.Zhao
  • 250
  • 3
  • 16
  • If I make the method static then I will not be able to access any of the methods of the outer class – Tungamirai Sep 15 '15 at 05:54
  • @Tungamirai Service must be static if you want put it to be an inner class. – Andy.Zhao Sep 15 '15 at 05:56
  • is there any way of accessing any outer class methods from a static inner class – Tungamirai Sep 15 '15 at 06:02
  • Activity and Service can communicate,http://stackoverflow.com/questions/2463175/how-to-have-android-service-communicate-with-activity .Your Service or Activity start a command then the other get this command and do what you want. – Andy.Zhao Sep 15 '15 at 06:12