2009-06-29 8 views
35

मुझे यकीन नहीं है कि एंड्रॉइड एमुलेटर इसके बूटिंग को पूरा करने के बाद एंड्रॉइड एप्लिकेशन को ऑटोस्टार्ट कैसे करें। क्या किसी के पास कोई कोड स्निपेट है जो मेरी मदद करेगा?एंड्रॉइड एप्लिकेशन को ऑटोस्टार्ट कैसे करें?

+0

@AdamC आप गलत कर रहे हैं - Prashast के जवाब देखें। –

+0

@ राजपंडियन मालिक को जवाब देना चाहिए यदि यह एक समाधान समाधान है, या टिप्पणियों में आपकी अपेक्षाओं का उल्लेख करें। यह दूसरों के लिए सहायक होगा। – naveejr

+0

http: // करनबाल्कर।कॉम/2014/01/ऑटोस्टार्ट-एप्लिकेशन-पर-बूट-इन-एंड्रॉइड/ –

उत्तर

12

यदि ऑटोस्टार्ट द्वारा आपका मतलब है कि फोन बूटअप पर ऑटो स्टार्ट है तो आपको BOOT_COMPLETED मंशा के लिए ब्रॉडकास्ट रिसीवर पंजीकृत करना चाहिए। एंड्रॉइड सिस्टम ब्रॉडकास्ट करता है कि एक बार बूट पूरा हो जाने का इरादा है।

एक बार जब आप उस इरादे को प्राप्त कर लेते हैं तो आप एक सेवा लॉन्च कर सकते हैं जो आप जो भी करना चाहते हैं कर सकते हैं।

ध्यान रखें कि फोन पर हर समय चल रही सेवा आमतौर पर एक बुरा विचार है क्योंकि यह निष्क्रिय होने पर भी सिस्टम संसाधनों को खाती है। आवश्यकता होने पर आपको अपनी सेवा/एप्लिकेशन लॉन्च करना चाहिए और फिर आवश्यक होने पर इसे रोकना चाहिए।

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

(बेशक आप अन्य सभी अनुमतियों है कि आपके एप्लिकेशन का उपयोग करता सूचीबद्ध करना चाहिए):

36

आप एक प्रकट अनुमति प्रविष्टि जोड़ने के लिए है।

फिर, ब्रॉडकास्ट रिसीवर कक्षा को लागू करें, यह सरल और तेज़ निष्पादन योग्य होना चाहिए। सबसे अच्छा तरीका यह है कि इस रिसीवर में आपकी सेवा उठाने के लिए अलार्म सेट करना है (अगर इसे लिखने के लिए प्राइस्ट लिखने के लिए जरूरी नहीं है)।

public class BootUpReceiver extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, MyService.class), PendingIntent.FLAG_UPDATE_CURRENT); 
    am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval, interval, pi); 
}} 

फिर, अपने मैनिफ़ेस्ट फ़ाइल के लिए एक रिसीवर वर्ग जोड़ें:

<receiver android:enabled="true" android:name=".receivers.BootUpReceiver" 
     android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </receiver> 
+0

अंतराल क्या है? – asmgx

+0

इस लाइन में "रिसीवर"। रिसीवर एंड्रॉइड: सक्षम = "सत्य" एंड्रॉइड: नाम = "। रिसीवर.बूटअप रिसीवर" त्रुटि देता है "अविश्वसनीय पैकेज रिसीवर" – asmgx

16

अपने AndroidManifest.xml जोड़ने के लिए संपादित RECEIVE_BOOT_COMPLETED अनुमति

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

संपादित अपने AndroidManifest.xml के लिए आवेदन-भाग अनुमति

नीचे
<receiver android:enabled="true" android:name=".BootUpReceiver" 
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> 
<intent-filter> 
    <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 
</receiver> 

अब गतिविधि में नीचे लिखें।

public class BootUpReceiver extends BroadcastReceiver{ 
@Override 
public void onReceive(Context context, Intent intent) { 
    Intent i = new Intent(context, MyActivity.class); 
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(i); 
} 
} 
+0

यह ठीक काम करता है। धन्यवाद! –

0

मैं हमेशा इस विषय के लिए यहां आ जाता हूं। मैं अपना कोड यहां डाल दूंगा ताकि मैं (या अन्य) अगली बार इसका उपयोग कर सकूं। (Phew मेरे भंडार कोड में खोजने से नफरत है)।

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

रिसीवर और सेवा जोड़ें::

<receiver android:enabled="true" android:name=".BootUpReceiver" 
     android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </receiver> 
    <service android:name="Launcher" /> 

बनाएं वर्ग लांचर:

public class Launcher extends Service { 
    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     new AsyncTask<Service, Void, Service>() { 

      @Override 
      protected Service doInBackground(Service... params) { 
       Service service = params[0]; 
       PackageManager pm = service.getPackageManager(); 
       try { 
        Intent target = pm.getLaunchIntentForPackage("your.package.id"); 
        if (target != null) { 
         service.startActivity(target); 
         synchronized (this) { 
          wait(3000); 
         } 
        } else { 
         throw new ActivityNotFoundException(); 
        } 
       } catch (ActivityNotFoundException | InterruptedException ignored) { 
       } 
       return service; 
      } 

      @Override 
      protected void onPostExecute(Service service) { 
       service.stopSelf(); 
      } 

     }.execute(this); 

     return START_STICKY; 
    } 
} 

वर्ग BootUpReceiver बनाएं एंड्रॉयड रिबूट के बाद कार्रवाई करने के लिए

अनुमति जोड़ें।

उदाहरण लांच MainActivity के लिए:

public class BootUpReceiver extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent target = new Intent(context, MainActivity.class); 
     target.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(target); 
    } 
} 
+0

<रिसीवर एंड्रॉइड: सक्षम = "सत्य" एंड्रॉइड पर त्रुटि: नाम = "बूटअप रिसीवर" अनदेखा वर्ग BootUpReceiver – asmgx

+0

@asmgx: मैंने जवाब अपडेट किया है। इसे वहां रखने के लिए भूल गए। – nafsaka

संबंधित मुद्दे