vendredi 29 mai 2015

How to start my application after device boot? [duplicate]

This question already has an answer here:

i want to develop an application that starts after device boot.

i use this code but it dose not worked.

i the manifest i use both android:name=".receiver.StartMyServiceAtBootReceiver" and android:name="StartMyServiceAtBootReceiver" but it dose not worked.

whats the problem ?

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://ift.tt/nIICcg"
    package="com.example.onstart"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <service android:name=".MySystemService" android:label="MySystemService">
             <intent-filter>
               <action android:name="com.example.onstart.MySystemService" />
              </intent-filter>
        </service>

        <receiver
             android:name=".receiver.StartMyServiceAtBootReceiver"
             android:enabled="true"
             android:exported="true"
             android:label="StartMyServiceAtBootReceiver">
            <intent-filter>
               <action android:name="android.intent.action.BOOT_COMPLETED" />
             </intent-filter>
        </receiver>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

StartMyServiceAtBootReceiver:

package com.example.onstart;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class StartMyServiceAtBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent serviceIntent = new Intent(context, MySystemService.class);
            context.startService(serviceIntent);
            Toast.makeText(context, "On Receive 1", Toast.LENGTH_LONG).show();
        }
    }
}

MySystemService:

package com.example.onstart;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MySystemService extends Service{
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
     public void onCreate() {
            super.onCreate();
     }
    @Override
    public void onStart(Intent intent, int startid)
    {
       Toast.makeText(getApplicationContext(), "On service start 1", Toast.LENGTH_LONG).show();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();

    }
}

Aucun commentaire:

Enregistrer un commentaire