Android应用程序在设备上运行后立即崩溃
我有一个android应用程序,它在我在android Studio中创建的模拟器上运行良好,但当我在设备上尝试它时,它不会运行。由于minSDKversion不兼容,该设备最初与应用程序不兼容,因此我在模拟器上运行了它。在对应用程序的Android应用程序在设备上运行后立即崩溃,android,Android,我有一个android应用程序,它在我在android Studio中创建的模拟器上运行良好,但当我在设备上尝试它时,它不会运行。由于minSDKversion不兼容,该设备最初与应用程序不兼容,因此我在模拟器上运行了它。在对应用程序的build.gradle进行更改后(将minsdkversion从18降级到17,以便在我的根设备上运行),我能够成功地将其安装到设备上,但一旦运行,它就会崩溃,并说“不幸的是,我的应用程序已停止运行”。在另一个API级别更高的设备(Moto G Android
build.gradle
进行更改后(将minsdkversion从18降级到17,以便在我的根设备上运行),我能够成功地将其安装到设备上,但一旦运行,它就会崩溃,并说“不幸的是,我的应用程序已停止运行”。在另一个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')
}
最初的版本是18岁。其他一切都没有改变。
有什么问题吗?有什么建议吗
以下是日志:
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)
主要活动:
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);
}
}
}
通知分析:
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);
}
}
}
}
NotificationListenerService
是您使用的,较低版本的API不支持该服务
适用于旧api(你能发布logcat吗?现在用logcat编辑吗?你能发布你的MainActivity吗?好像你在代码中的某个地方使用了新的API调用。这就是为什么它在新的操作系统设备上工作,而不是在旧的设备上。你能在第29行显示你的MainActivity.java吗?如果我像最初一样将minsdkversion设置为18,那么NotificationListenerService问题将是解决了,是的,但它不会在我的设备上运行,因为它的API是17。我如何完成这两件事?我以为根目录可以帮助我解决min API问题,但它不知道我可以让这个应用程序阻止在设备上安装其他应用程序吗?这个应用程序将获得通知访问权限,所以我想通过它的通知通道阻止安装d帮助。有什么建议吗?你不能阻止在Android上安装其他应用程序。没有API