0

I have to schedule invoking of an already install test project on the device after 10 sec once the button is click. For that I have created an AlarmReceiver and my TaskService which actually is invoking the test project.

After running the app, nothing happens after the button is clicked. I don't know what is wrong but service doesn't performs the job even after 10 sec.

Below is my code which I am trying. The Activity class:

// Create an anonymous implementation of OnClickListener
private OnClickListener HTListener = new OnClickListener() {
    public void onClick(View v) {
        btnStartSchedule(v);
    } 
};


public void btnStartSchedule(View v) {
    try {
        AlarmManager alarms = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

        Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
        intent.putExtra(AlarmReceiver.ACTION_ALARM, AlarmReceiver.ACTION_ALARM);


        final PendingIntent pIntent = PendingIntent.getBroadcast(this, 1234567, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        alarms.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10000, pIntent);

        Toast.makeText(ScrapeActivity.this, "Alarm Started", Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

The AlarmReceiver class :

public class AlarmReceiver extends BroadcastReceiver {

public static String ACTION_ALARM = "com.alarmmanager.alarm";

 @Override
 public void onReceive(Context context, Intent intent) {

    Log.i("Alarm Receiver", "Entered");
    Toast.makeText(context, "Entered", Toast.LENGTH_SHORT).show();

    Bundle bundle = intent.getExtras();
    String action = bundle.getString(ACTION_ALARM);

    if (action.equals(ACTION_ALARM)) {
        Log.i("Alarm Receiver", "If loop");
        Intent inService = new Intent(context, LaunchService.class);
        context.startService(inService);
    } else {
        Log.i("Alarm Receiver", "Else loop");
        Toast.makeText(context, "Else loop", Toast.LENGTH_SHORT).show();
    }
 }

}

And finally the LaunchService class:

public class LaunchService extends IntentService {

public LaunchService(String name) {
    super(name);
    // TODO Auto-generated constructor stub
}


@Override
protected void onHandleIntent(Intent intent) {
    Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.my.testproject");
    startActivity(LaunchIntent);
}

}

I have also mention the receiver and service inside the manifest file :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.pb.activity"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/PB_APP"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.my.pb.activity.ScrapeActivity"
        android:label="@string/PB_APP" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver
        android:name=".AlarmReceiver"
        android:process=":remote" >
    </receiver>

    <service android:name=".LaunchService" >
    </service>
</application>

I don't understand whats the issue coz there's no exception in the Logcat. As I click the button, i just see the toast message saying Alarm Started. Please help.

nikis
  • 11,166
  • 2
  • 35
  • 45
roger_that
  • 9,493
  • 18
  • 66
  • 102

1 Answers1

3

You have a typo at the Manifest, you declared AlaramReceiver instead of .AlarmReceiver there.

nikis
  • 11,166
  • 2
  • 35
  • 45
  • Using `android:name=".AlarmReceiver"` also doesn't help. – roger_that May 01 '14 at 08:11
  • @VaibhavShukla It should work. Post updated manifest completely – nikis May 01 '14 at 08:18
  • @VaibhavShukla well, everything looks fine. Take a look at the logcat, showing all messages. Do you see an entry that broadcast is sent? – nikis May 01 '14 at 10:09
  • No. I don't see anything on logs related to broadcast or any other Alarm related logs. However I found this line in logcat but that doesn't seems to be an exception. 05-01 15:48:38.483: W/ActivityManager(850): Unable to start service Intent { flg=0x4 cmp=com.my.pb.activity/com.expedia.pb.alarmreceiver.AlarmReceiver (has extras) } U=0: not found – roger_that May 01 '14 at 10:21
  • @VaibhavShukla in such a case use full-qualified names for both `Receiver` and `Service` in `Manifest` – nikis May 01 '14 at 10:25
  • Yes. Now I get an exception finally. 05-01 16:11:14.696: E/AndroidRuntime(5100): FATAL EXCEPTION: main 05-01 16:11:14.696: E/AndroidRuntime(5100): java.lang.RuntimeException: Unable to instantiate service com.my.pb.service.LaunchService: java.lang.InstantiationException: can't instantiate class com.my.pb.service.LaunchService; no empty constructor – roger_that May 01 '14 at 10:42