私が受け取るエラーは「残念ながら、サービスが機能しなくなりました」です。ここで、サービスは私のアプリの名前です。質問は以前の質問と似ていますが、ここではまったく別のことをしています - SMS メッセージを読もうとしています。
MainActivity.java
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start, stop;
start = (Button) findViewById(R.id.Button1);
stop = (Button) findViewById(R.id.Button2);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent service = new Intent(MainActivity.this, MyService.class);
startService(service);
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent name = new Intent(MainActivity.this, MyService.class);
stopService(name);
}
});
}
}
私のサービスファイル、MyService.java:
public class MyService extends Service {
final static String ACTION = "NotifyServiceAction";
final static String STOP_SERVICE = "";
final static int RQS_STOP_SERVICE = 1;
private SMSreceiver mSMSreceiver;
private IntentFilter mIntentFilter;
SMSreceiver notifyServiceReceiver;
private static final int MY_NOTIFICATION_ID=1;
private NotificationManager notificationManager;
private Notification myNotification;
private final String myBlog = "http://android-er.blogspot.com/";
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
notifyServiceReceiver = new SMSreceiver ();
mSMSreceiver = new SMSreceiver();
mIntentFilter = new IntentFilter();
mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(mSMSreceiver, mIntentFilter);
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("MyService","Service started");
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION);
registerReceiver(notifyServiceReceiver, intentFilter);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.i("MyService","Service destroyed");
this.unregisterReceiver(mSMSreceiver);
super.onDestroy();
}
private class SMSreceiver extends BroadcastReceiver
{
private final String TAG = this.getClass().getSimpleName();
@Override
public void onReceive(Context context, Intent intent)
{
Bundle extras = intent.getExtras();
String strMessage = "";
if ( extras != null )
{
Object[] smsextras = (Object[]) extras.get( "pdus" );
for ( int i = 0; i < smsextras.length; i++ )
{
SmsMessage smsmsg = SmsMessage.createFromPdu((byte[])smsextras[i]);
String strMsgBody = smsmsg.getMessageBody().toString();
String strMsgSrc = smsmsg.getOriginatingAddress();
strMessage += "SMS from " + strMsgSrc + " : " + strMsgBody;
Log.i(TAG, strMessage);
}
}
}
}
}
サービスマニフェスト.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="yash.my.service"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="yash.my.service.MyService$SMSreceiver">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity
android:name="yash.my.service.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyService"
android:enabled="true" >
</service>
</application>
</manifest>
SMS を AVD に送信すると、エラーが表示されます。ログも投稿します:
04-20 16:00:45.555: E/Trace(1085): error opening trace file: No such file or directory (2)
04-20 16:00:46.793: D/gralloc_goldfish(1085): Emulator without GPU emulation detected.
04-20 16:00:50.203: I/MyService(1085): Service started
04-20 16:01:05.933: D/AndroidRuntime(1085): Shutting down VM
04-20 16:01:05.933: W/dalvikvm(1085): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-20 16:01:05.943: E/AndroidRuntime(1085): FATAL EXCEPTION: main
04-20 16:01:05.943: E/AndroidRuntime(1085): java.lang.RuntimeException: Unable to instantiate receiver yash.my.service.MyService.SMSreceiver: java.lang.ClassNotFoundException: Didn't find class "yash.my.service.MyService.SMSreceiver" on path: /data/app/yash.my.service-1.apk
04-20 16:01:05.943: E/AndroidRuntime(1085): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2357)
04-20 16:01:05.943: E/AndroidRuntime(1085): at android.app.ActivityThread.access$1500(ActivityThread.java:141)
04-20 16:01:05.943: E/AndroidRuntime(1085): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
04-20 16:01:05.943: E/AndroidRuntime(1085): at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 16:01:05.943: E/AndroidRuntime(1085): at android.os.Looper.loop(Looper.java:137)
04-20 16:01:05.943: E/AndroidRuntime(1085): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-20 16:01:05.943: E/AndroidRuntime(1085): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 16:01:05.943: E/AndroidRuntime(1085): at java.lang.reflect.Method.invoke(Method.java:511)
04-20 16:01:05.943: E/AndroidRuntime(1085): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-20 16:01:05.943: E/AndroidRuntime(1085): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-20 16:01:05.943: E/AndroidRuntime(1085): at dalvik.system.NativeStart.main(Native Method)
04-20 16:01:05.943: E/AndroidRuntime(1085): Caused by: java.lang.ClassNotFoundException: Didn't find class "yash.my.service.MyService.SMSreceiver" on path: /data/app/yash.my.service-1.apk
04-20 16:01:05.943: E/AndroidRuntime(1085): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
04-20 16:01:05.943: E/AndroidRuntime(1085): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
04-20 16:01:05.943: E/AndroidRuntime(1085): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
04-20 16:01:05.943: E/AndroidRuntime(1085): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2352)
04-20 16:01:05.943: E/AndroidRuntime(1085): ... 10 more
04-20 16:01:06.203: D/dalvikvm(1085): GC_CONCURRENT freed 106K, 8% free 2692K/2916K, paused 75ms+2ms, total 213ms
コードを更新した後の新しい LOGCAT:
04-20 16:33:41.163: I/MyService(1304): Service started
04-20 16:33:56.355: D/dalvikvm(1304): newInstance failed: no <init>()
04-20 16:33:56.355: D/AndroidRuntime(1304): Shutting down VM
04-20 16:33:56.363: W/dalvikvm(1304): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-20 16:33:56.483: E/AndroidRuntime(1304): FATAL EXCEPTION: main
04-20 16:33:56.483: E/AndroidRuntime(1304): java.lang.RuntimeException: Unable to instantiate receiver yash.my.service.MyService$SMSreceiver: java.lang.InstantiationException: can't instantiate class yash.my.service.MyService$SMSreceiver; no empty constructor
04-20 16:33:56.483: E/AndroidRuntime(1304): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2357)
04-20 16:33:56.483: E/AndroidRuntime(1304): at android.app.ActivityThread.access$1500(ActivityThread.java:141)
04-20 16:33:56.483: E/AndroidRuntime(1304): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
04-20 16:33:56.483: E/AndroidRuntime(1304): at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 16:33:56.483: E/AndroidRuntime(1304): at android.os.Looper.loop(Looper.java:137)
04-20 16:33:56.483: E/AndroidRuntime(1304): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-20 16:33:56.483: E/AndroidRuntime(1304): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 16:33:56.483: E/AndroidRuntime(1304): at java.lang.reflect.Method.invoke(Method.java:511)
04-20 16:33:56.483: E/AndroidRuntime(1304): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-20 16:33:56.483: E/AndroidRuntime(1304): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-20 16:33:56.483: E/AndroidRuntime(1304): at dalvik.system.NativeStart.main(Native Method)
04-20 16:33:56.483: E/AndroidRuntime(1304): Caused by: java.lang.InstantiationException: can't instantiate class yash.my.service.MyService$SMSreceiver; no empty constructor
04-20 16:33:56.483: E/AndroidRuntime(1304): at java.lang.Class.newInstanceImpl(Native Method)
04-20 16:33:56.483: E/AndroidRuntime(1304): at java.lang.Class.newInstance(Class.java:1319)
04-20 16:33:56.483: E/AndroidRuntime(1304): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2352)
04-20 16:33:56.483: E/AndroidRuntime(1304): ... 10 more
04-20 16:33:56.613: D/dalvikvm(1304): GC_CONCURRENT freed 119K, 9% free 2692K/2928K, paused 75ms+3ms, total 214ms