我刚开始 android 开发 2 天,这是我无法解决的错误“不幸的是,[App] 已停止”。
这就是我打算用APP做的事情。
显示将告诉用户按 ok 应用程序将开始制作日志文件的 UI。
当按下 OK 按钮时,它将在后台运行(不需要 UI)并等待来自其他应用程序的通知,并将通知中的文本写入日志文件。
这是我的 AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notificationnotifier"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.notificationnotifier.GetNotification"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.notificationnotifier.MonitorNotification"
android:label="@string/app_name" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
这是 GetNotification.java
public class GetNotification extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_notification);
Button okButton = (Button) findViewById(R.id.OKButton);
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent getNotification = new Intent("com.example.notificationnotifier.MonitorNotification");
startActivity(getNotification);
}
}) ;
Button cancel = (Button) findViewById(R.id.Cancel);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
}) ;
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
}
这是 MonitorNotification.java
import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.view.accessibility.AccessibilityEvent;
public class MonitorNotification extends AccessibilityService{
@Override
public void onAccessibilityEvent(AccessibilityEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void onInterrupt() {
// TODO Auto-generated method stub
}
@Override
protected void onServiceConnected() {
// TODO Auto-generated method stub
// super.onServiceConnected();
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.feedbackType = 1;
info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
info.notificationTimeout = 100;
setServiceInfo(info);
}
}
这是 logcat [更新]
02-04 10:31:17.947: E/Trace(1108): error opening trace file: No such file or directory (2)
02-04 10:31:18.648: D/gralloc_goldfish(1108): Emulator without GPU emulation detected.
02-04 10:31:21.128: D/AndroidRuntime(1108): Shutting down VM
02-04 10:31:21.148: W/dalvikvm(1108): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
02-04 10:31:21.169: E/AndroidRuntime(1108): FATAL EXCEPTION: main
02-04 10:31:21.169: E/AndroidRuntime(1108): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.notificationnotifier/com.example.notificationnotifier.MonitorNotification}: java.lang.ClassCastException: com.example.notificationnotifier.MonitorNotification cannot be cast to android.app.Activity
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.os.Looper.loop(Looper.java:137)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-04 10:31:21.169: E/AndroidRuntime(1108): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 10:31:21.169: E/AndroidRuntime(1108): at java.lang.reflect.Method.invoke(Method.java:511)
02-04 10:31:21.169: E/AndroidRuntime(1108): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-04 10:31:21.169: E/AndroidRuntime(1108): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-04 10:31:21.169: E/AndroidRuntime(1108): at dalvik.system.NativeStart.main(Native Method)
02-04 10:31:21.169: E/AndroidRuntime(1108): Caused by: java.lang.ClassCastException: com.example.notificationnotifier.MonitorNotification cannot be cast to android.app.Activity
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
02-04 10:31:21.169: E/AndroidRuntime(1108): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
02-04 10:31:21.169: E/AndroidRuntime(1108): ... 11 more
02-04 10:31:23.268: I/Process(1108): Sending signal. PID: 1108 SIG: 9