无法实例化服务

时间:2016-07-03 19:47:40

标签: android service broadcastreceiver

以下是我面临的错误:

  /com.DownToLunch E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.DownToLunch, PID: 14615
  java.lang.RuntimeException: Unable to instantiate service com.DownToLunch.Networking.RecieveSMS$MessageService: java.lang.InstantiationException: can't instantiate class com.DownToLunch.Networking.RecieveSMS$MessageService; no empty constructor
     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2688)
     at android.app.ActivityThread.access$1800(ActivityThread.java:155)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:136)
     at android.app.ActivityThread.main(ActivityThread.java:5433)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:515)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
     at dalvik.system.NativeStart.main(Native Method)
  Caused by: java.lang.InstantiationException: can't instantiate class com.DownToLunch.Networking.RecieveSMS$MessageService; no empty constructor
     at java.lang.Class.newInstanceImpl(Native Method)
     at java.lang.Class.newInstance(Class.java:1208)
     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2685)
     at android.app.ActivityThread.access$1800(ActivityThread.java:155) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:136) 
     at android.app.ActivityThread.main(ActivityThread.java:5433) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
     at dalvik.system.NativeStart.main(Native Method) 

这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.DownToLunch">

  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
  <uses-permission android:name="android.permission.READ_SMS" />

  <application
    android:name=".app.AppController"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/Theme.NoTitle">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Otp"
        android:theme="@style/Theme.NoTitle" />
    <activity android:name=".DetectOtp"></activity>
    <receiver android:name="com.DownToLunch.Networking.RecieveSMS">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
    <service
        android:enabled="true"
        android:name=".Networking.RecieveSMS$MessageService">
    </service>
  </application>
</manifest>

这是课程,我已经定义了我的服务:

public class RecieveSMS extends BroadcastReceiver {

    Context mcontext;
    String phoneNumber;
    String message;
    @Override
    public void onReceive(Context context, Intent intent) {
        final Bundle bundle = intent.getExtras();

        mcontext=context;
        String senderNum;
        try {

            if (bundle != null) {

                final Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {

                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                     phoneNumber = currentMessage.getDisplayOriginatingAddress();

                    senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();
                    Log.d("Message Body",message);
                    //send();
                    Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);
                    Intent ie=new Intent(mcontext, MessageService.class);
                    mcontext.startService(ie);

                } // end for loop
            } // bundle is null

        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" + e);

        }
    }
    public class MessageService extends Service {

        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }
        @Override
        public void onCreate() {

        }
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // TODO Auto-generated method stub
            send();
            return super.onStartCommand(intent, flags, startId);
        }

        // Send an Intent with an action named "custom-event-name". The Intent
        // sent should
        // be received by the ReceiverActivity.
        public void send() {
            //Log.d(TAG, "MainActivity.send()");
            Intent sendableIntent = new Intent("otp");
            sendableIntent.putExtra("message",message);
            LocalBroadcastManager.getInstance(mcontext).
                    sendBroadcast(sendableIntent);

        }

    }


}

我在这里收到了otp并试图在吐司中展示它:

public class DetectOtp extends AppCompatActivity {

    String message;
    private IntentFilter receiveFilter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detect_otp);
        receiveFilter = new IntentFilter("otp");
        LocalBroadcastManager.getInstance(this).
                registerReceiver(handler, receiveFilter);

    }
    private BroadcastReceiver handler = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
           // Bundle b =intent.getExtras();
           // b = getIntent().getExtras();
            message = intent.getStringExtra("message");
            Log.d("Message",message);
            Toast.makeText(DetectOtp.this,
                    message, Toast.LENGTH_LONG).show();
        }
    };
}

我在哪里弄错了?在启动服务以使用LocalBroadcastManager发送值时,它显示此错误:

  

com.DownToLunch.Networking.RecieveSMS $ MessageService:
  java.lang.InstantiationException:无法实例化类
  com.DownToLunch.Networking.RecieveSMS $ MessageService;没空?

1 个答案:

答案 0 :(得分:1)

替换:

public class MessageService extends Service

使用:

static public class MessageService extends Service

你不能让Service成为一个内在的阶级,就像你现在拥有它一样。