티스토리 뷰

마켓에서 어플리케이션을 받아서 설치하거나, 삭제할 때도 이벤트가 발생합니다.
이 이벤트를 받으려면 BroadcastReceiver를 이용하면 됩니다.


#1.
일단, AndroidManifest.xml 파일에 다음과 같이 BroadcastReceiver를 등록합니다.

<application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name" >

        <activity

                android:label="@string/app_name"

                android:name=".MainActivity" >

                <intent-filter >

                        <action android:name="android.intent.action.MAIN" />

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

                </intent-filter>

        </activity>

  

        <receiver android:name=".PackageReceiver">

                <intent-filter>

                        <action android:name="android.intent.action.PACKAGE_ADDED"/>

                        <action android:name="android.intent.action.PACKAGE_REMOVED"/>

                        <action android:name="android.intent.action.PACKAGE_REPLACED"/>

                        <data android:scheme="package"/>

                </intent-filter>

        </receiver>

  

</application>

 



#2.
BroadcastReceiver를 상속받은 PackageReceiver 클래스를 생성합니다.

 

package plugin.prototype.app;

 

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

 

public class PackageReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        String packageName = intent.getData().getSchemeSpecificPart();
        String action = intent.getAction();
         
        if(action.equals(Intent.ACTION_PACKAGE_ADDED))
        {
            Log.d("", "[snowdeer] Package ADDED : " + packageName);
        }
        else if(action.equals(Intent.ACTION_PACKAGE_REMOVED))
        {
            Log.d("", "[snowdeer] Package REMOVED : " + packageName);
        }
    }
}

 


#3.
그리고 실제 Activity에서 등록해서 사용하면 됩니다.

private PackageReceiver mPackageReceiver = new PackageReceiver();
registerReceiver( mPackageReceiver, new IntentFilter( Intent.ACTION_PACKAGE_ADDED ) );

 

 

또는

 

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL);
intentFilter.addDataScheme("package");
registerReceiver(br, intentFilter);

 



또한, Activity 종료시에

unregisterReceiver( mPackageReceiver );

를 해주시는 것을 잊지 마세요. ^^;;

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함