广播接收器在仿真器上工作,但不在设备上工作

时间:2015-06-16 12:36:34

标签: android broadcastreceiver

我有一个应用程序,我正在使用广播接收器来启动服务。

当我在ADV(模拟器)上运行它时工作正常,但是当我尝试在设备上运行它时,无法工作。

我尝试使用android 2.3.3,4.0.3,5.0和许多不同的设备,但没有成功。

清单

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />      
<application>
<receiver android:name="com.polifrete.polifreteFunctions.MyReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON"/>

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>
<service
    android:name="com.polifrete.polifreteFunctions.NotificationService" >
</service>
</application>

RECEIVER

public class MyReceiver extends BroadcastReceiver {

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

    Log.i("Script", "Passou RECEIVER");
    Intent i = new Intent(context, NotificationService.class);
    context.startService(i);

}
}

服务

public class NotificationService extends Service {

public static final int time = 15 * 1000;

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

@Override
public void onCreate() {
    Log.i("Script", "Passou SERVICE CREATE");
    super.onCreate();
}

@Override
public void onStart(Intent intent, int startId) {
    Log.i("Script", "Passou SERVICE START");
}

@Override
public void onDestroy() {
    Log.i("Script", "Passou SERVICE DESTROY");
    super.onDestroy();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("Script", "Passou SERVICE COMMAND");
    final Thread t = new Thread() {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(time);
                    showNotification();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    };
    t.start();
    return Service.START_STICKY;
}

public void showNotification() {

    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder n = new NotificationCompat.Builder(this)
            .setContentTitle("Polifrete")
            .setContentText("Test Notification.")
            .setSmallIcon(
                    com.polifrete.polifreteandroid.R.drawable.ic_actionbar)
            .setContentIntent(pIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    notificationManager.notify((int)(Math.random()*10000), n.build());

}
}

1 个答案:

答案 0 :(得分:0)

您的应用是否支持外部存储? 如果是,则将其更改为仅内部