sách gpt4 ăn đã đi

android - android应用程序可以只有广播接收器和没有 Activity 的服务吗

In lại 作者:太空狗 更新时间:2023-10-29 15:55:23 25 4
mua khóa gpt4 giày nike

android 应用程序可以只有广播接收器和服务而没有 Activity 吗?如果这是可能的,我该如何调用广播接收器? Android 系统自动调用 broadcsat 接收器 ?

Broadcastreceiver代码

  public class CheckReceiver extends BroadcastReceiver {

public Context con;

@Ghi đè
public void onReceive(Context context, Intent intent) {
// TODO Phương thức tự động tạo stub

Toast.makeText(context, "Received", Toast.LENGTH_LONG).show();
// add PhoneStateListener
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener,
PhoneStateListener.LISTEN_CALL_STATE);

con = context;
}

class PhoneCallListener extends PhoneStateListener {

private boolean isPhoneCalling = false;

String LOG_TAG = "LOGGING 123";

@Ghi đè
public void onCallStateChanged(int state, String incomingNumber) {

if (TelephonyManager.CALL_STATE_RINGING == state) {
// phone ringing
Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
}

if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
Log.i(LOG_TAG, "OFFHOOK");

isPhoneCalling = true;
}

if (TelephonyManager.CALL_STATE_IDLE == state) {
// run when class initial and phone call ended, need detect flag
// from CALL_STATE_OFFHOOK
Log.i(LOG_TAG, "IDLE");
if (isPhoneCalling) {
Log.i(LOG_TAG, "restart app");
Intent start = new Intent(con, CheckService.class);
con.startService(start);
isPhoneCalling = false;
}

}
}
}
}

服务准则是

public class CheckService extends Service{

@Ghi đè
public IBinder onBind(Intent arg0) {
// TODO Phương thức tự động tạo stub
trả về giá trị null;
}

@Ghi đè
public void onStart(Intent intent, int startId) {
// TODO Phương thức tự động tạo stub
super.onStart(intent, startId);
Intent dialogIntent = new Intent(CheckService.this,SmartDialog.class);
dialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);


//Toast.makeText(CheckService.this, "Serive", Toast.LENGTH_LONG).show();
}

}

Android list 文件是



package="com.mkyong.android"
android:versionCode="1"
android:versionName="1.0" >






<ứng>
android:icon="@drawable/ic_launcher"
android:label="@string/tên_ứng_dụng" >





当我尝试运行此代码时,接收器未启动。任何帮助都会非常有帮助

câu trả lời hay nhất

从 Honeycomb 开始,BroadcastReceivers 以停止状态安装,并且在应用程序实际运行之前不会触发,即您需要一个 Activity 至少运行一次。这篇 Commonsware 博客文章对此进行了深入解释:

Broadcast Regression Confirmed

Android 3.1

关于android - android应用程序可以只有广播接收器和没有 Activity 的服务吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12315325/

25 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress