在开始之前,我想澄清一下,我已经阅读过类似主题的先前答案(主要是这个),即使在评论中给出的答案也对我不起作用(在 intent.getAction() 之前添加 intent != null没用)。
我正在尝试为玛雅星座做前台服务。该服务的目的是从 SQLite 数据库中读取星座符号和预测,并且每当它收到带有以下内容的文本消息时:“MAYA dd/MM/yyyy”(DOB),该服务将获取信息并发送自动发短信。我已经让它作为后台服务正常工作,但我想给它一个通知,以便用户可以看到该服务是否正在运行。
我已经按照其他教程(教程 1和教程 2)制作了前台服务,并且它工作正常且没有错误。
我现在的问题是,每当我启动服务、停止服务并最终清除 ram(杀死应用程序)时,我总是在我的服务的第 57 行上得到 Nullpointer 异常(无论我有什么代码,它总是第 57 行)。在我的手机上,它给了我一条消息“不幸的是,HoroscopoMaya 已停止工作。” 两次!
清单.xml
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ServicioAstralMaya"
android:exported="false" />
</application>
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
MainActivity.java:
public void startService( View view )
{
if( bRunning == false )
{
Intent intent = new Intent( MainActivity.this, ServicioAstralMaya.class );
intent.setAction( Constants.ACTION.START_FOREGROUND_ACTION );
startService( intent );
txtVStatServ.setText( "INVOCADOS " );
bRunning = true;
}
}//END startService( View )
public void stopService( View view )
{
if( bRunning == true )
{
Intent intent = new Intent( MainActivity.this, ServicioAstralMaya.class );
intent.setAction( Constants.ACTION.STOP_FOREGROUND_ACTION );
startService( intent );
txtVStatServ.setText( "DESCANSANDO " );
bRunning = false;
}
}//END stopService( View )
ServicioAstralMaya.java(服务):
@Override
public int onStartCommand( Intent intent, int flags, int startID )
{
if( intent != null && intent.getAction().equals( Constants.ACTION.START_FOREGROUND_ACTION
) )
{
Log.i( TAG, "Servicio iniciado!" );
isRunning = true;
startForeground( Constants.NOTIFICATION_ID.SERVICE_ID, getCompatNotification() );
}else if( intent != null && intent.getAction().equals( Constants.ACTION.STOP_FOREGROUND_ACTION ) )
{
Log.i( TAG, "Servicio detenido!" );
isRunning = false;
stopForeground( true );
stopSelf();
}
return START_STICKY;
}
private Notification getCompatNotification()
{
NotificationCompat.Builder builder = new NotificationCompat.Builder( this );
builder.setSmallIcon( R.drawable.homonculus_32 )
.setContentTitle( "Horoscopos Maya" )
.setTicker( "Astros Conectados" )
.setWhen( System.currentTimeMillis() );
Intent startIntent = new Intent( this, MainActivity.class );
PendingIntent contentIntent = PendingIntent.getActivity( this, 0, startIntent,
0 );
builder.setContentIntent( contentIntent );
Notification notification = builder.build();
return notification;
}//END Notification getCompatNotification()
@Override
public void onDestroy()
{
super.onDestroy();
isRunning = false;
}
常量.java
public interface ACTION
{
public static final String START_FOREGROUND_ACTION = "edu.ito.jmhg" +
".horoscopomaya.action.START_FOREGROUND";
public static final String STOP_FOREGROUND_ACTION = "edu.ito.jmhg" +
".horoscopomaya.action.STOP_FOREGROUND";
}
public interface NOTIFICATION_ID
{
public static final int SERVICE_ID = 1;
}
第 57 行是这样的:Log.i( TAG, "Servicio iniciado!" );
isRunning 和 bRunning 是检查服务是否正在运行,以便在 MainActivity 中显示状态:
bRunning = isServiceRunning( ServicioAstralMaya.class );
private boolean isServiceRunning( Class<?> serviceClass )
{
ActivityManager manager = (ActivityManager)getSystemService( Context.ACTIVITY_SERVICE );
for( ActivityManager.RunningServiceInfo service : manager.getRunningServices( Integer
.MAX_VALUE ) )
{
if( serviceClass.getName().equals( service.service.getClassName() ) ) {
return true;
}
}
return false;
}//END boolean isServiceRunning( Class<?> )
并在服务中:public static boolean isRunning;
我什至尝试调用 stopService 并在方法中执行stopForeground(true);
and ,但仍然无济于事。错误仍然是我的服务类的第 57 行。stopSelf();
onDestroy()
日志猫:
FATAL EXCEPTION: main
Process: edu.ito.jmhg.horoscopomaya, PID: 21642
java.lang.RuntimeException: Unable to start service edu.ito.jmhg.horoscopomaya.ServicioAstralMaya@426d11c8 with null: java.lang.NullPointerException
...
Caused by: java.lang.NullPointerException at edu.ito.jmhg.horoscopomaya.ServicioAstralMaya.onStartCommand(ServicioAstralMaya.java:57)
我正在使用根植于三星 Grand Prime 的 Android Studio 2 运行 Android KitKat 4.4.4 (API 19)。
编辑
我不知道这是否有点相关,但我确实注意到了一些关于 Android Studio 的东西。无论我做了什么更改,当我运行应用程序时,我都会收到“无需更改部署”的通知
事件簿:
19:32:47 Executing tasks: [:app:assembleDebug]
19:33:50 Gradle build finished in 1m 3s 59ms
19:33:51 No changes to deploy
我已经更改了我的服务的代码,并且行错误仍然是 57,现在) )
就在之后if( intent.getAction().equals( Constants.ACTION.START_FOREGROUND_ACTION
ServicioAstralMaya.java(服务):
@Override
public int onStartCommand( Intent intent, int flags, int startID )
{
if( intent != null && intent.getAction() != null )
{
if( intent.getAction().equals( Constants.ACTION.START_FOREGROUND_ACTION
) )
{
Log.i( TAG, "Servicio iniciado!" );
isRunning = true;
startForeground( Constants.NOTIFICATION_ID.SERVICE_ID, getCompatNotification() );
}else if( intent.getAction().equals( Constants.ACTION.STOP_FOREGROUND_ACTION ) )
{
Log.i( TAG, "Servicio detenido!" );
isRunning = false;
stopForeground( true );
stopSelf();
}
}
return START_STICKY;
}
此外,由于我已更改为 Android 2,因此我的 gradle 已更新,并且出现了这个奇怪的错误,我不得不将 build.gradle 中的 buildToolsVersion 从更改buildToolsVersion "24.0 rc2"
为buildToolsVersion "23.0.2"
构建.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "edu.ito.jmhg.horoscopomaya"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
}