2

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

广播接收者代码

  public class CheckReceiver extends BroadcastReceiver {

        public Context con;

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method 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";

            @Override
            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{

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method 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清单文件是

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mkyong.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" /> 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

       <receiver android:name="CheckReceiver"></receiver>
        <service android:name="CheckService"></service>
        <activity android:name="SmartDialog" android:theme="@android:style/Theme.Dialog"></activity>
    </application>

当我尝试运行此代码接收器时没有开始。任何帮助都会非常有帮助

4

2 回答 2

8

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

广播回归确认

安卓 3.1

于 2012-09-07T09:39:19.783 回答
0
public class FlashApkclass extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

     }
}

在您的 manifest.xml 文件中,将您的 android:name 设置为

<receiver android:name="com.example.ambrecentral.server.FlashApkclass"
                android:exported="false">
        <intent-filter>
         // register you broadcast receiver to list broadcast 
           <action android:name="com.example.android.APP_CLOUD_DELETE_APK"/>
        </intent-filter>

</receiver>

如果你想广播

broadcast.setAction(BROADCAST_ACTION);
sendBroadcast(broadcast);

BROADCAST_ACTION 在哪里

public static String BROADCAST_ACTION = "com.example.android.APP_CLOUD_DELETE_APK";    
于 2012-09-07T09:44:26.503 回答