1
我有一個應用程序,我使用廣播接收器來啓動服務。廣播接收器在仿真器上工作,但不在設備上
當我在ADV(仿真器)上運行它時,它工作正常,但是當我嘗試在設備上運行它時,無法正常工作。
我試着用android 2.3.3,4.0.3,5.0和許多diferents設備,沒有成功。
清單
<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>
接收機
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());
}
}
在將設備安裝到設備上後,是否至少在啓動之前手動啓動應用程序一次? – helleye
已經完成了它,我關閉了應用程序,並手動啓動它,沒有任何東西 –
您的應用程序是否有任何活動? –