Skip to content

Commit

Permalink
Fixes/service not starting (#42)
Browse files Browse the repository at this point in the history
* Fix for service not starting and possible fix for ClassNotFoundException
- JobIntentService requires a different method to start
- Class was not saved properly... Probably, because Class<?>.toString() returns Class<?>.getName() so it's actually the same

* Updated README.md file
  • Loading branch information
CremaLuca authored and Cogno-Marco committed Dec 31, 2019
1 parent 51eb9ac commit 5ec03a4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ In the single module `build.gradle` add:
```java
dependencies {
...
implementation 'com.github.Cogno-IDU:smslibrary:v-1.0'
implementation 'com.github.Cogno-IDU:smslibrary:v2.1'
}
```
## Usage
Expand Down Expand Up @@ -65,7 +65,7 @@ SMSManager.getInstance().sendMessage(msg, new SMSSentListener() {
```
If you are planning to use SMS delivery reports, you can have a callback for those too:
```java
SMSHandler.getInstance().sendMessage(msg, new SMSDeliveredListener() {
SMSManager.getInstance().sendMessage(msg, new SMSDeliveredListener() {
@Override
public void onSMSDelivered(SMSMessage message, SMSMessage.DeliveredState deliveredState) {
// Do something
Expand All @@ -79,8 +79,16 @@ In order to register the application to be called on message reception you have
to register a custom listener service that extends `SMSReceivedServiceListener`.
To do so you have to receive a class (not an instance):
```java
SMSHandler.getInstance().setReceivedListener(MyCustomReceiver.class);
SMSManager.getInstance().setReceivedListener(MyCustomReceiver.class);
```
This service must be registered in the manifest inside the `<application>` tags like this:
```java
<service
android:name=".MyCustomReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BIND_JOB_SERVICE" />
```
When a message arrives the overridden method `onMessageReceived` will be called.

### Using your own message format
Expand Down
7 changes: 3 additions & 4 deletions smslibrary/src/main/java/com/eis/smslibrary/SMSManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import android.content.Intent;
import android.content.IntentFilter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.eis.communication.CommunicationManager;
import com.eis.smslibrary.listeners.SMSDeliveredListener;
import com.eis.smslibrary.listeners.SMSReceivedServiceListener;
import com.eis.smslibrary.listeners.SMSSentListener;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import it.lucacrema.preferences.PreferencesManager;

/**
Expand Down Expand Up @@ -171,7 +170,7 @@ private PendingIntent setupNewDeliverReceiver(final @NonNull SMSMessage message,
* @param context the context used to set the listener
*/
public <T extends SMSReceivedServiceListener> void setReceivedListener(Class<T> receivedListenerClassName, Context context) {
PreferencesManager.setString(context, SMSReceivedBroadcastReceiver.SERVICE_CLASS_PREFERENCES_KEY, receivedListenerClassName.toString());
PreferencesManager.setString(context, SMSReceivedBroadcastReceiver.SERVICE_CLASS_PREFERENCES_KEY, receivedListenerClassName.getName());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

import androidx.core.app.JobIntentService;
import it.lucacrema.preferences.PreferencesManager;

/**
Expand All @@ -20,6 +22,7 @@ public class SMSReceivedBroadcastReceiver extends BroadcastReceiver {

public static final String INTENT_MESSAGE_TAG = "SMSMessage";
public static final String SERVICE_CLASS_PREFERENCES_KEY = "ApplicationServiceClass";
private static final int SCHEDULING_JOB_ID = 420;

/**
* Parses message and calls listener
Expand Down Expand Up @@ -74,17 +77,18 @@ private SmsMessage createMessageFromPdu(Object smsData, String format) {
*/
private void callApplicationService(Context context, SMSMessage message) {
Class<?> listener = null;
String preferencesClassName = PreferencesManager.getString(context, SERVICE_CLASS_PREFERENCES_KEY);
try {
listener = Class.forName(PreferencesManager.getString(context, SERVICE_CLASS_PREFERENCES_KEY));
listener = Class.forName(preferencesClassName);
} catch (ClassNotFoundException e) {
Log.e("SMSReceiver", "Service class to wake up could not be found");
Log.e("SMSReceiver", "Service class to wake up could not be found: " + preferencesClassName);
}
if (listener == null)
return;

Intent serviceIntent = new Intent(context, listener);
Intent serviceIntent = new Intent();
serviceIntent.putExtra(INTENT_MESSAGE_TAG, message);
context.startService(serviceIntent);
JobIntentService.enqueueWork(context, listener, SCHEDULING_JOB_ID, serviceIntent);

}
}

0 comments on commit 5ec03a4

Please sign in to comment.