2013-01-23 59 views
1

我用廣播接收器開始我就啓動應用程序,但它沒有啓動應用程序沒有啓動的設備引導

這裏是我的代碼

清單代碼:

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

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="9" /> 
<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" > 

    <receiver android:name="com.copy.MyBroadcastreceiver" > 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 

    <activity 
     android:name="com.copy.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> 

廣播級代碼:

public class MyBroadcastreceiver extends BroadcastReceiver 
{ 

@Override 
public void onReceive(Context context, Intent intent) 
{ 
    Intent startServiceIntent = new Intent(context, MainActivity.class); 
    startServiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startService(startServiceIntent); 
} 

} 

MainActivity類別:

public class MainActivity extends Activity 
{ 
ListView lView; 
private ArrayList results = new ArrayList(); 

@SuppressWarnings("unchecked") 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    lView = (ListView) findViewById(R.id.list1); 
    PackageManager pm = this.getPackageManager(); 

    Intent intent = new Intent(Intent.ACTION_MAIN, null); 
    intent.addCategory(Intent.CATEGORY_LAUNCHER); 


    List<ResolveInfo> list = pm.queryIntentActivities(intent,PackageManager.PERMISSION_GRANTED); 
    for (ResolveInfo rInfo : list) 
    { 
     results.add(rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());  
     Log.w("Installed Applications", rInfo.activityInfo.applicationInfo.loadLabel(pm).toString()); 
    } 
    lView.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, results)); 
    lView.setOnItemClickListener(new OnItemClickListener() 
    { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) 
     {    
      String x = (String) arg0.getItemAtPosition(arg2); 
      Toast.makeText(getApplicationContext(), "This is "+lView.getItemAtPosition(arg2), Toast.LENGTH_SHORT).show(); 

     } 
    }); 
} 


} 
+0

有在LogCat中的東西? – RvdK

+0

請求時間失敗:java.net.SocketException:協議不支持的地址系列 – Anirudh

+0

當MainActivity不是'Service'時,爲什麼使用'startService()'?你需要使用'startActivity()'來代替。 – m0skit0

回答

3

您正在使用startService()MainActivity不是Service。您需要改用startActivity()

public class MyBroadcastreceiver extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     Intent startActivityIntent = new Intent(context, MainActivity.class); 
     startActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(startActivityIntent); 
    } 
} 
+0

由於應用程序處於STOP模式,因此這將不起作用,因此API 3.1中停止的應用程序不會收到任何廣播,除非手動啓動應用程序。 – Noman

0

開始嘗試的活動,而不是服務: -

Intent i = new Intent(context, MainActivity.class); 
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
context.startActivity(i); 
0

是的,叫startActivity

@Override 
public void onReceive(Context context, Intent intent) { 

    Intent startActivityIntent = new Intent(context, MainActivity.class); 
     startActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(startActivityIntent); 
    } 

瞭解更多請參考這個鏈接 http://blog.burnayev.com/2009/08/android-development-how-to-launch.html

相關問題