2012-03-12 23 views
0

我之前問我怎樣才能在服務顯示一個AlertDialog,我在尋找,我發現這個答案:Here顯示AlertDialog在服務調用的活動

,但我不知道我怎麼能叫從服務活動?? 我寫了這段代碼,我不知道爲什麼會出現錯誤:應用程序意外停止!

誰能幫助pleassssssssse這些問題花了大量的時間:「(

這是我的代碼:

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

public class MyAlarmService extends Service { 

    @Override 
    public void onCreate() { 
     // TODO Auto-generated method stub 
     Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG) 
       .show(); 

    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG) 
       .show(); 
     return null; 
    } 

    @Override 
    public void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG) 
       .show(); 
    } 

    @Override 
public void onStart(Intent intent, int startId) { 
// TODO Auto-generated method stub 
super.onStart(intent, startId); 
Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show(); 

Intent i = new Intent (this, ShowingDialogActivity.class); 
startActivity(i); 



} 

    @Override 
    public boolean onUnbind(Intent intent) { 
     // TODO Auto-generated method stub 
     Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG) 
       .show(); 
     return super.onUnbind(intent); 
    } 

} 

// This is ShowingDialogActivity 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 

public class ShowingDialog extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     //For Notification 
     final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
      //For Notification 
      alertDialog.setTitle("Conformation"); 
      alertDialog.setMessage("Are you sure you want to Expand Report Region?"); 


        alertDialog.setButton("Yes", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          // here you can add functions 
          // Sending a Message to server that the plaintiff wants to expand report region 

         } 
        }); 

        alertDialog.setButton2("No", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
         // here you can add functions 
         // Do nothing 


         } 
        }); 

        alertDialog.setIcon(android.R.drawable.ic_dialog_alert); 
        alertDialog.show(); 

    } 




} 

// the Manifest 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".AndroidAlarmService" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity android:name=".ShowingDialog" 
        android:theme="@android:style/Theme.Translucent" 
        ></activity> 



     <service android:name=".MyAlarmService" 

      /> 
    </application> 
</manifest> 

誰能告訴我什麼是錯,此代碼??????? ???

+0

@Zelimir你能幫助嗎? – Samiah 2012-03-12 18:06:10

回答

0

是活動AndroidAlarmService出現在你的應用程序,如Android清單規定?

<activity android:name=".AndroidAlarmService" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
1

Upate您服務: 開始從後臺的活動中,你必須設置標誌Intent.FLAG_FROM_BACKGROUND

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

public class MyAlarmService extends Service { 

    @Override 
    public void onCreate() { 
     // TODO Auto-generated method stub 
     Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG) 
       .show(); 

    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG) 
       .show(); 
     return null; 
    } 

    @Override 
    public void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG) 
       .show(); 
    } 

    @Override 
public void onStart(Intent intent, int startId) { 
// TODO Auto-generated method stub 
super.onStart(intent, startId); 
Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show(); 

Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(
           Intent.CATEGORY_LAUNCHER).setClassName("YOUR_PACKAGE_NAME", 
           "YOUR_PACKAGE_NAME.ShowingDialog.class").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 
           .addFlags(Intent.FLAG_FROM_BACKGROUND).setComponent(new ComponentName("YOUR_PACKAGE_NAME", 
          "YOUR_PACKAGE_NAME.ShowingDialog.class")); 
       getApplicationContext().startActivity(intent); 



} 

    @Override 
    public boolean onUnbind(Intent intent) { 
     // TODO Auto-generated method stub 
     Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG) 
       .show(); 
     return super.onUnbind(intent); 
    } 

} 
} 
相關問題