SMS服务需要的类或接口?

时间:2014-09-21 15:27:38

标签: java android android-studio android-service development-environment

我想创建一个简单的服务,但是当我尝试在清单中调用我的ServiceSMS.java类时,它会告诉我"类或接口预期"。

我的清单:

<?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();
        // ... 
    }
}

这是我的短信接收者:

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) {

            }
        });

    }
}

我的虫子在哪里?

2 个答案:

答案 0 :(得分:1)

这里有很多问题。

更改

  <receiver class="com.exemple.romain.SMSReceiver" android:name=".SMSReceiver">

<receiver class="com.example.romain.SMSReceiver" android:name=".SMSReceiver">

您有示例而不是示例

你需要把

 <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />

之前<activity>

将您的服务声明更改为

 <service android:name="com.example.romain.ServiceSMS"/>

答案 1 :(得分:0)

将其更改为

<service android:name=".myapplication.ServiceSMS"/>
清单中的