我在设备

时间:2016-03-24 05:04:02

标签: android

我有一个Android应用程序,它在我在Android Studio中创建的模拟器上运行良好但在我在设备上尝试时它不会运行。由于minSDKversion不兼容,设备最初与应用程序不兼容,因此我在模拟器上运行它。在对应用程序的build.gradle进行更改(将minsdkversion从18降级到17以便它在我的root设备上运行)之后,我能够在设备上成功安装它,但是一旦运行它崩溃说&# 34;不幸的是MyApplication已停止运行"。在另一台具有更高API级别的设备上运行它(Moto G Android 5.1)我没有遇到这个问题,但是Micromax Canvas A110Q给出了这个问题。

这是build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "19.1.0"
    defaultConfig {
        applicationId "com.example.amrit.myapplication"
        minSdkVersion 16
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
    packagingOptions {
        exclude 'META-INF/NOTICE' // will not include NOTICE file
        exclude 'META-INF/LICENSE' // will not include LICENSE file
        exclude 'META-INF/DEPENDENCIES' // will not include LICENSE file
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
    compile files('libs/httpclient-4.5.2.jar')
    compile files('libs/httpcore-4.4.4.jar')
}

minsdkversion最初是18.没有其他任何改变。 可能是什么问题呢?有什么建议吗?

这是logcat:

03-24 10:36:42.148 17838-17838/com.example.amrit.myapplication
E/AndroidRuntime: FATAL EXCEPTION: main

java.lang.NoClassDefFoundError: com.example.amrit.myapplication.NotificationAnalyze

        at com.example.amrit.myapplication.MainActivity.onCreate(MainActivity.java:29)

        at android.app.Activity.performCreate(Activity.java:5122)

        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)

        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)

        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)

        at android.app.ActivityThread.access$600(ActivityThread.java:156)

        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)

        at android.os.Handler.dispatchMessage(Handler.java:99)

        at android.os.Looper.loop(Looper.java:153)

        at android.app.ActivityThread.main(ActivityThread.java:5297)

        at java.lang.reflect.Method.invokeNative(Native Method)

        at java.lang.reflect.Method.invoke(Method.java:511)

        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)

        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)

        at dalvik.system.NativeStart.main(Native Method)

MainActivity:

public class MainActivity extends Activity {

    private TextView txtView;
    private NotificationReceiver nReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtView = (TextView) findViewById(R.id.textView);
        nReceiver = new NotificationReceiver();
        Intent s = new Intent(MainActivity.this, NotificationAnalyze.class);
        startService(s);
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.example.amrit.myapplication.NOTIFICATION_LISTENER_EXAMPLE");
        registerReceiver(nReceiver, filter);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(nReceiver);
    }



    public void buttonClicked(View v){

        if(v.getId() == R.id.btnCreateNotify){
            NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            NotificationCompat.Builder ncomp = new NotificationCompat.Builder(this);
            ncomp.setContentTitle("My Notification");
            ncomp.setContentText("Notification here");
            ncomp.setTicker("Notification here");
            ncomp.setSmallIcon(R.drawable.ic_launcher);
            ncomp.setAutoCancel(true);
            nManager.notify((int) System.currentTimeMillis(), ncomp.build());
        }
        else if(v.getId() == R.id.btnClearNotify){
            Intent i = new Intent("com.example.amrit.myapplication.NOTIFICATION_LISTENER_SERVICE_EXAMPLE");
            i.putExtra("command","clearall");
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            sendBroadcast(i);
            Log.d("clearing", i.toString());
        }
        else if(v.getId() == R.id.btnListNotify){
            Intent i = new Intent("com.example.amrit.myapplication.NOTIFICATION_LISTENER_SERVICE_EXAMPLE");
            i.putExtra("command","list");
            sendBroadcast(i);
        }


    }

    class NotificationReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String temp = intent.getStringExtra("notification_event") + "\n" + txtView.getText();
            txtView.setText(temp);
        }
    }



}

NotificationAnalyze:

public class NotificationAnalyze extends NotificationListenerService {

    /**
     * A constructor is required, and must call the super IntentService(String)
     * constructor with a name for the worker thread.
     */

    public static String TAG = "NotificationListenerTesting";
    private NLServiceReceiver nlservicereciver;
    @Override
    public void onCreate(){
        super.onCreate();
        nlservicereciver = new NLServiceReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.example.amrit.myapplication.NOTIFICATION_LISTENER_SERVICE_EXAMPLE");
        registerReceiver(nlservicereciver, filter);
        Log.d("create", "herer");
    }
    @Override
     public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(nlservicereciver);
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        TAG = "onNotificationPosted";
        Log.d(TAG, "id = " + sbn.getId() + "Package Name" + sbn.getPackageName() +
                "Post time = " + sbn.getPostTime() + "Tag = " + sbn.getTag());
    }
    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        TAG = "onNotificationRemoved";
        Log.d(TAG, "id = " + sbn.getId() + "Package Name" + sbn.getPackageName() +
                "Post time = " + sbn.getPostTime() + "Tag = " + sbn.getTag());

    }
    class NLServiceReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getStringExtra("command").equals("clearall")){
                Log.d(TAG, "here");
                NotificationAnalyze.this.cancelAllNotifications();
            }
            else if(intent.getStringExtra("command").equals("list")){
                Log.d(TAG, "here2");
                Intent i1 = new  Intent("com.example.amrit.myapplication.NOTIFICATION_LISTENER_EXAMPLE");
                i1.putExtra("notification_event","=====================");
                sendBroadcast(i1);
                int i=1;
                for (StatusBarNotification sbn : NotificationAnalyze.this.getActiveNotifications())
                {
                    Intent i2 = new  Intent("com.example.amrit.myapplication.NOTIFICATION_LISTENER_EXAMPLE");
                    String packageName = sbn.getPackageName();
                    String text = "", apkPath="";
                    text = "Notification Text : " + sbn.getNotification().tickerText + "\n\n";
                    text = text + "Package name of notification :  " + packageName + "\n\n";
                    try
                    {
                        Process process = Runtime.getRuntime().exec("pm path " + packageName);
                        BufferedReader bufferedReader = new BufferedReader(
                                new InputStreamReader(process.getInputStream()));

                        apkPath = bufferedReader.readLine().substring(8);
                        Log.d("APK path : ", apkPath);
                        text = text + "Check for 'install' in Notification Text, If so \n\n";
                        text = text + "APK Path : " + apkPath + "\n\n";
                        text = text + "Sending apk for upload \n";


                    }
                    catch (Exception e)
                    {
                        Log.e(TAG, e.toString());
                    }


                    i2.putExtra("notification_event", text);
                    sendBroadcast(i2);
                    i++;

                    new UploadData().execute(apkPath);

                }
                Intent i3 = new  Intent("com.example.amrit.myapplication.NOTIFICATION_LISTENER_EXAMPLE");
                i3.putExtra("notification_event","===== Notification List ====");
                sendBroadcast(i3);

            }

        }
    }
}

1 个答案:

答案 0 :(得分:0)

您使用is available starting from API level 18

NotificationListenerService,不支持较低版本的API。

对于旧api(< 18),您可以使用AccessibilityServiceHere's a manual你怎么做。