Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SMS服务需要的类或接口?_Java_Android_Android Studio_Android Service_Development Environment - Fatal编程技术网

Java SMS服务需要的类或接口?

Java SMS服务需要的类或接口?,java,android,android-studio,android-service,development-environment,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

我想创建一个简单的服务,但当我试图调用清单中的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>
这里是我的短信接收器:

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

            }
        });

    }
}
package com.example.romain.myapplication;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.widget.Toast;
导入android.speech.tts.TextToSpeech;
导入android.speech.tts.utranceProgressListener;
导入android.telephony.sms消息;
导入静态java.util.Locale.FRANCE;
公共类SMSReceiver扩展BroadcastReceiver实现TextToSpeech.OnInitListener{
静态文本到语音tts;
@凌驾
公共void onReceive(上下文、意图){
字符串phoneNumber=null;
字符串ACTION\u RECEIVE\u SMS=“android.provider.Telephony.SMS\u RECEIVED”;
if(intent.getAction().equals(ACTION\u RECEIVE\u SMS)){
Bundle=intent.getExtras();
Object[]pdus=(Object[])bundle.get(“pdus”);
最终SmsMessage[]消息=新SmsMessage[pdus.length];
对于(int j=0;j-1){
最后一个字符串messageBody=消息[0]。getMessageBody();
phoneNumber=消息[0]。getDisplayOriginatingAddress();
}
String test=“Tu as reçu un nouveau sms de”+电话号码;
Intent speechIntent=新Intent();
setClass(上下文,ReadTheMessage.class);
speechIntent.putExtra(“消息”,测试);
speechIntent.addFlags(Intent.FLAG_活动_新任务| Intent.FLAG_活动_排除_近期);
语境。开始触觉(言语意图);
}
}
@凌驾
公共无效onInit(int i){
setOnPatternanceProgressListener(新的话语进程Listener(){
@凌驾
公共void onStart(字符串s){
}
@凌驾
公共无效onDone(字符串s){
System.out.println(“Fait”);
}
@凌驾
公共void onError(字符串s){
}
});
}
}

请问我的bug在哪里?

这里有很多问题

改变

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


您有示例而不是示例

你需要把

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

并将您的服务声明更改为

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

将其更改为

<service android:name=".myapplication.ServiceSMS"/>


在清单

中,任何更改,都会出现相同的问题:/Just Shumany,我可以编译并运行我的应用程序,但错误仍然存在。问题已解决,但我无法发布屏幕截图。我没有10个声誉。谢谢你,纳宾!当我试着纠正你的错误时,我也遇到了同样的问题。谢谢你的回答没有看到你的Android项目的结构,很难说错误在哪里。我猜这可能与相对路径有关,比如android:name=“.ServiceSMS”,或者您还没有从您的
广播接收器启动ServiceSMS
服务。用你的项目结构编辑你的帖子,或者至少向我们展示
SMSReceiver
类的代码。
 <service android:name="com.example.romain.ServiceSMS"/>
<service android:name=".myapplication.ServiceSMS"/>