0

我需要一個內容解析程序來使用在後臺運行的服務設置日曆中的事件,而我沒有打開任何應用程序的活動,因此我無法獲取任何上下文或我們需要使用GetContentResolver的活動對象,引導我在這裏可以做些什麼,謝謝。在沒有任何活動運行的情況下在服務中使用GetContentResolver

+0

獲得應用程序上下文 –

+0

@SuryaPrakashKushawah我是新到Android請你告訴我該怎麼做 –

+0

一個'Service'是'Context'。直接調用'getContentResolver()'。 –

回答

1
public class AppName extends Application 
{ 
    private static Context mContext; 

    @Override 
    public void onCreate() 
    { 
     super.onCreate(); 

     mContext = this; 
    } 

    public static Context getContext() 
    { 
     return mContext; 
    } 
} 

在服務類

public class MyIntentService extends IntentService { 
    // TODO: Rename actions, choose action names that describe tasks that this 
    // IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS 
    private static final String ACTION_FOO = "com.jvvnl.app.action.FOO"; 
    private static final String ACTION_BAZ = "com.jvvnl.app.action.BAZ"; 

    // TODO: Rename parameters 
    private static final String EXTRA_PARAM1 = "com.jvvnl.app.extra.PARAM1"; 
    private static final String EXTRA_PARAM2 = "com.jvvnl.app.extra.PARAM2"; 

    public MyIntentService() { 
    super("MyIntentService"); 
    } 

    /** 
    * Starts this service to perform action Foo with the given parameters. If 
    * the service is already performing a task this action will be queued. 
    * 
    * @see IntentService 
    */ 
    // TODO: Customize helper method 
    public static void startActionFoo(Context context, String param1, String param2) { 
    Intent intent = new Intent(context, MyIntentService.class); 
    intent.setAction(ACTION_FOO); 
    intent.putExtra(EXTRA_PARAM1, param1); 
    intent.putExtra(EXTRA_PARAM2, param2); 
    context.startService(intent); 
    } 

    /** 
    * Starts this service to perform action Baz with the given parameters. If 
    * the service is already performing a task this action will be queued. 
    * 
    * @see IntentService 
    */ 
    // TODO: Customize helper method 
    public static void startActionBaz(Context context, String param1, String param2) { 
    Intent intent = new Intent(context, MyIntentService.class); 
    intent.setAction(ACTION_BAZ); 
    intent.putExtra(EXTRA_PARAM1, param1); 
    intent.putExtra(EXTRA_PARAM2, param2); 
    context.startService(intent); 
    } 

    @Override protected void onHandleIntent(Intent intent) { 
    if (intent != null) { 
    ContentResolver resolver=getContentResolver(); 
    } 
    } 

    /** 
    * Handle action Foo in the provided background thread with the provided 
    * parameters. 
    */ 
    private void handleActionFoo(String param1, String param2) { 
    // TODO: Handle action Foo 
    throw new UnsupportedOperationException("Not yet implemented"); 
    } 

    /** 
    * Handle action Baz in the provided background thread with the provided 
    * parameters. 
    */ 
    private void handleActionBaz(String param1, String param2) { 
    // TODO: Handle action Baz 
    throw new UnsupportedOperationException("Not yet implemented"); 
    } 
相關問題