1

新しい画像、ビデオ、オーディオ、またはapkがユーザーによって何時でも挿入されるとすぐにユーザーに通知する必要があるアプリケーションで作業しています彼がそれをダウンロードしても転送してもかまいません)、ファイルマネージャアプリケーションと同様です。どこから始めたらよいかわかりません。ありがとうAndroid:最近追加されたAndroidデバイスのメディアをファイルマネージャーのように手に入れよう

答えて

2

新しい画像、音声、またはビデオがデバイスに追加されたときに、ユーザーに通知する完全なコードです。それは誰かを助けるかもしれない。また、私は新しいメディアが追加されるとすぐにプッシュ通知を表示しています。

EDITEDメディアが追加されたときに
1.Notifyユーザーのみ、削除されていない(解決された問題は以下の通りです)。

public class MediaTrackerService extends Service { 

@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    // images 
    getContentResolver().registerContentObserver(
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
      true, new ContentObserver(new Handler()) { 
       @Override 
       public void onChange(boolean selfChange) { 
        super.onChange(selfChange); 

        Media media = readFromMediaStore(
          getApplicationContext(), 
          MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

        File file = new File(media.file.getPath()); 
        Date fileData = new Date(file.lastModified()); 

        if (System.currentTimeMillis() - fileData.getTime() > 3000) { 

        } else { 

         if (!media.file.getPath().contains("WhatsApp")) { 
          String saved = media.file.getName(); 

          // deduce bitmap before using 
          Bitmap bitmap = BitmapFactory 
            .decodeFile(media.file.getPath()); 

          Intent intent = new Intent(); 
          intent.setDataAndType(Uri.fromFile(media.file), 
            "image/*"); 
          PendingIntent pendingIntent = PendingIntent 
            .getActivity(getApplicationContext(), 
              0, intent, 0); 

          Notification n = new Notification.Builder(
            getApplicationContext()) 
            .setStyle(
              new Notification.BigPictureStyle() 
                .bigPicture(bitmap)) 
            .setContentTitle(
              "New Image File For You") 
            .setContentText("File Name :" + saved) 
            .setSmallIcon(R.drawable.alert_icon_1) 
            .setContentIntent(pendingIntent) 
            .setAutoCancel(true).build(); 

          NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

          notificationManager.notify(0, n); 
         } 
        } 
       } 

       @Override 
       public void onChange(boolean selfChange, Uri uri) { 
        super.onChange(selfChange, uri); 
       } 
      }); 
    // audio 
    getContentResolver().registerContentObserver(
      android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
      true, new ContentObserver(new Handler()) { 
       @Override 
       public void onChange(boolean selfChange) { 
        super.onChange(selfChange); 
        Media media = readFromMediaStore(
          getApplicationContext(), 
          MediaStore.Audio.Media.EXTERNAL_CONTENT_URI); 

        File file = new File(media.file.getPath()); 
        Date fileData = new Date(file.lastModified()); 

        if (System.currentTimeMillis() - fileData.getTime() > 3000) { 

        } else { 

         if (!media.file.getPath().contains("WhatsApp")) { 

          String saved = media.file.getName(); 

          Bitmap bitmap = BitmapFactory 
            .decodeFile(media.file.getPath()); 

          Intent intent = new Intent(); 
          intent.setDataAndType(Uri.fromFile(media.file), 
            "audio/*"); 
          PendingIntent pendingIntent = PendingIntent 
            .getActivity(getApplicationContext(), 
              0, intent, 0); 

          Notification n = new Notification.Builder(
            getApplicationContext()) 
            .setStyle(
              new Notification.BigPictureStyle() 
                .bigPicture(null)) 
            .setContentTitle(
              "New audio File For You") 
            .setContentText("File Name :" + saved) 
            .setSmallIcon(R.drawable.alert_icon_1) 
            .setContentIntent(pendingIntent) 
            .setAutoCancel(true).build(); 

          NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

          notificationManager.notify(0, n); 
         } 
        } 
       } 

       @Override 
       public void onChange(boolean selfChange, Uri uri) { 
        super.onChange(selfChange, uri); 
       } 
      }); 
    // video 
    getContentResolver().registerContentObserver(
      android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI, 
      true, new ContentObserver(new Handler()) { 
       @Override 
       public void onChange(boolean selfChange) { 
        super.onChange(selfChange); 
        Media media = readFromMediaStore(
          getApplicationContext(), 
          MediaStore.Video.Media.EXTERNAL_CONTENT_URI); 

        File file = new File(media.file.getPath()); 
        Date fileData = new Date(file.lastModified()); 

        if (System.currentTimeMillis() - fileData.getTime() > 3000) { 

        } else { 
         if (!media.file.getPath().contains("WhatsApp")) { 

          String saved = media.file.getName(); 

          Intent intent = new Intent(); 
          intent.setDataAndType(Uri.fromFile(media.file), 
            "video/*"); 
          PendingIntent pendingIntent = PendingIntent 
            .getActivity(getApplicationContext(), 
              0, intent, 0); 

          Notification n = new Notification.Builder(
            getApplicationContext()) 
            .setStyle(
              new Notification.BigPictureStyle() 
                .bigPicture(null)) 
            .setContentTitle(
              "New Video File For You") 
            .setContentText("File Name :" + saved) 
            .setSmallIcon(R.drawable.alert_icon_1) 
            .setContentIntent(pendingIntent) 
            .setAutoCancel(true).build(); 

          NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

          notificationManager.notify(0, n); 
         } 
        } 
       } 

       @Override 
       public void onChange(boolean selfChange, Uri uri) { 
        super.onChange(selfChange, uri); 
        System.out 
          .println("here is the service change EXTERNAL 1 params" 
            + " " + uri); 
       } 
      }); 
    return START_STICKY; 
} 

private Long readLastDateFromMediaStore(Context context, Uri uri) { 
    Cursor cursor = context.getContentResolver().query(uri, null, null, 
      null, "date_added DESC"); 
    Long dateAdded = (long) -1; 
    if (cursor.moveToNext()) { 
     dateAdded = cursor.getLong(cursor 
       .getColumnIndexOrThrow(MediaColumns.DATE_ADDED)); 
    } 
    cursor.close(); 
    return dateAdded; 
} 

private Media readFromMediaStore(Context context, Uri uri) { 
    Cursor cursor = context.getContentResolver().query(uri, null, null, 
      null, "date_added DESC"); 
    Media media = null; 
    if (cursor.moveToNext()) { 
     int dataColumn = cursor.getColumnIndexOrThrow(MediaColumns.DATA); 
     String filePath = cursor.getString(dataColumn); 
     int mimeTypeColumn = cursor 
       .getColumnIndexOrThrow(MediaColumns.MIME_TYPE); 
     String mimeType = cursor.getString(mimeTypeColumn); 
     media = new Media(new File(filePath), mimeType); 
    } 
    cursor.close(); 
    return media; 
} 

private class Media { 
    private File file; 

    private String type; 

    public Media(File file, String type) { 
     this.file = file; 
     this.type = type; 
    } 

    public String getType() { 
     return type; 
    } 

    public File getFile() { 
     return file; 
    } 
} 

}

とあなたのオブザーバーonDestroy()を登録解除することを忘れないでください。

getContentResolver().unregisterContentObserver(yourObserverObject); 
+0

これは完璧に動作していただきありがとうございます。しかし、私はアプリを殺すと私はバックグラウンドでも結果が欲しいのですか? –

+0

@HarinKaklotar上記は、バックグラウンドで実行されるサービスクラス 'MediaTrackerService'の内部で行われます。アプリを強制終了すると、その結果に影響します。 –

+0

はい私はそれを知っています。ユーザーがアプリを強制終了した後、そのユーザーが音楽ファイルをデバイスに追加すると、ユーザーはどのように通知することができますか? 私たちがサービスでBroadcastReceiverを使用しているとしたら、デバイス内に新しいAudioファイルを取得するためにBroadcastReceiverで何を伝えたいのですか? –

1

これは一般的に不可能です。あなたのアプリは他のアプリの動作を察知する権利はありません。ユーザーが自分のアプリを通じてinternal storageのアプリの部分に何かをダウンロードした場合、そのコンテンツにアクセスすることはできません(利用可能にしない限り)、そのコンテンツが追加されたことがわかりません。

それ以上に、APIレベル24(Android 7.0)より前には、プロセスを常時実行しようとする以外に、これを達成する手段がありません。これは、システムのRAM消費量などの点で、ユーザにとっては良くありません。一部のユーザーは、アプリケーションが常にプロセスを実行しようとすると非常に苛立ちを覚え、アプリストアの評価の形で創造的な方法で刺激を表明することがあります。

すべてのことを言っているが、公的にアクセス可能なコンテンツ(主external storage)のために、最も簡単な解決策は、MediaStoreContentProviderを監視するためにContentObserverを設定するであろう。 Android 7.0以降では、JobSchedulerを使用して同じことを行うことができ、ContentObserverの状態を維持するためにプロセスを実行する必要はありません。

+0

あなたのenlightmentに感謝します。私はContentObserver –

+0

を使って欲望の仕事を達成しました。また、私は希望の仕事を達成して答えを投稿しましたが、私は直面しています。メディアが追加されても削除されないと通知するだけです。このコードは、どちらの場合でも実行されます。 –

+0

@bhanukaushik:申し訳ありませんが、すべての変更(追加、変更、削除)について通知されます。何らかのメディアが追加されたかどうかは、あなたが判断するのはあなた次第です。これは、あなたがやっていることをやってみるアプリがほとんどない理由の1つです。 – CommonsWare

関連する問題