我一直在研究一個應用程序,該應用程序用於接收短信並使用廣播接收器來顯示一個Toast,並且我有一個沒有目的的活動,構建apk並在我的手機上運行,應用程序在收到短信時沒有響應(沒有Toast顯示),儘管剩餘的代碼與以前相同。任何人都可以幫助我,我很困難,不能幫助自己閱讀數百個答案。我研究了我應該創建一個服務,但仍然沒有吐司出現。下面是我的代碼。即使不希望自動殺死活動,我的應用程序中也不能有任何GUI。如何在沒有GUI的情況下通過BroadcastReceiver使用Android服務接收短信
BroadcastReceiver.java
package com.test.testservice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver {
public static final String SMS_BUNDLE = "pdus";
private static final String LOG = "SmsBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Bundle intentExtras = intent.getExtras();
if (intentExtras != null) {
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
if (sms != null)
{
String smsMessageStr = "";
for (int i = 0; i < sms.length; ++i)
{
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
String smsBody = smsMessage.getMessageBody().toString();
String address = smsMessage.getOriginatingAddress();
smsMessageStr += "SMS From: " + address + "\n";
smsMessageStr += smsBody + "\n";
}
Toast.makeText(context, smsMessageStr, Toast.LENGTH_LONG).show();
//MyService objService=new MyService();
//objService.startService(intent);
//objService.stopService(intent);
Intent myIntent = new Intent(context, MyService.class);
//myIntent.putExtra("Sender", Sender);
//myIntent.putExtra("Fullsms", Fullsms);
context.startService(myIntent);
}
}
}
}
MyService.java
package com.test.testservice;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
private static final String LOG = "MyService";
@Override
public boolean stopService(Intent name) {
if (super.stopService(name))
{
Toast.makeText(this,"HELLO stopService",Toast.LENGTH_LONG).show();
Log.i(LOG, "stopService");
return true;
}
else return false;
}
@Override
public ComponentName startService(Intent service) {
return super.startService(service);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this,"HELLO onCreate",Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Toast.makeText(this,"HELLO onStart",Toast.LENGTH_LONG).show();
}
}
的AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.testservice">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name=".SmsReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.REBOOT"/>
</intent-filter>
</receiver>
</application>
<service android:name="com.test.testservice.service.MyService"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
</manifest>
我可以在沒有任何GUI的情況下創建服務並將其安裝到手機上嗎?然後創建一個單獨的應用程序來運行該服務,一旦服務運行我卸載第二個應用程序? –