Я хочу создать простой сервис, но когда я пытаюсь вызвать свой класс ServiceSMS.java в манифесте, он говорит мне, что «класс или интерфейс ожидается».Класс или интерфейс, ожидаемый для службы SMS?
Мой манифест:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.romain.myapplication">
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<service android:name="ServiceSMS"/>
<activity
android:name="com.example.romain.myapplication.MyActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver class="com.exemple.romain.SMSReceiver" android:name=".SMSReceiver">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity
android:name="com.example.romain.myapplication.ReadTheMessage">
</activity>
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
</manifest>
И мой класс ServiceSMS:
package com.example.romain.myapplication;
import android.app.IntentService;
import android.content.Intent;
public class ServiceSMS extends IntentService {
public static final String LOG_TAG = "ServiceSMS";
public ServiceSMS() {
super("ServiceSMS");
}
@Override
protected void onHandleIntent(Intent workIntent) {
String dataString = workIntent.getDataString();
// ...
}
}
здесь мой SMS приемник:
package com.example.romain.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.telephony.SmsMessage;
import static java.util.Locale.FRANCE;
public class SMSReceiver extends BroadcastReceiver implements TextToSpeech.OnInitListener {
static TextToSpeech tts;
@Override
public void onReceive(Context context, Intent intent) {
String phoneNumber = null;
String ACTION_RECEIVE_SMS = "android.provider.Telephony.SMS_RECEIVED";
if (intent.getAction().equals(ACTION_RECEIVE_SMS)) {
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[]) bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int j = 0; j < pdus.length; j++) { messages[j] = SmsMessage.createFromPdu((byte[]) pdus[j]); } if (messages.length > -1) {
final String messageBody = messages[0].getMessageBody();
phoneNumber = messages[0].getDisplayOriginatingAddress();
}
String test = "Tu as reçu un nouveau sms de "+phoneNumber;
Intent speechIntent = new Intent();
speechIntent.setClass(context, ReadTheMessage.class);
speechIntent.putExtra("MESSAGE", test);
speechIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(speechIntent);
}
}
@Override
public void onInit(int i) {
tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String s) {
}
@Override
public void onDone(String s) {
System.out.println("Fait");
}
@Override
public void onError(String s) {
}
});
}
}
Где моя ошибка, пожалуйста?
Не видя структуру вашего Android проекта, трудно сказать, где ошибка является. Я предполагаю, что это может быть связано с относительным путем, например 'android: name =. ServiceSMS", или что вы не запустили ** ServiceSMS ** «Сервис» из вашего «BroadcastReceiver». Отредактируйте свой пост в структуре вашего проекта или, по крайней мере, покажите нам коды класса 'SMSReceiver'. – ChuongPham