2016-04-06 28 views
1

我有一個Receiver類,用於檢查手機是否從充電器插入或拔出。我想在拔下手機時播放聲音(使用通知)。當我從後臺刪除程序時,My Receiver Class失敗

以下是我的課程,它的工作原理是正確的。但是,當我從「啓動最近的程序」窗口關閉我的程序,只有這部分力量關閉(我仍然可以看到吐司「連接」),爲什麼?

MyReceiver.java

public class MyReceiver extends BroadcastReceiver { 
static int num = 0; 
public static SharedPreferences sharedPreferences; 
Notification notification = new Notification(); 
@Override 
public void onReceive(Context context, Intent intent) { 

    String action = intent.getAction(); 

    if(action.equals(Intent.ACTION_POWER_CONNECTED)) { 
     Toast.makeText(context, "CONNECTED", Toast.LENGTH_LONG).show(); 
    } 
    else if(action.equals(Intent.ACTION_POWER_DISCONNECTED)) { 

     Toast.makeText(context, "" + num, Toast.LENGTH_LONG).show(); 

     NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
     String strRingtonePreference = sharedPreferences.getString("song", null); 
     Toast.makeText(context, "" +strRingtonePreference, Toast.LENGTH_LONG).show(); 
     notification.sound = Uri.parse(strRingtonePreference); 
     nm.notify(0, notification); 
     num++; 
    } 
} 

,這是我mainActivity.java

public class MainActivity extends Activity { 
public String path = ""; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Button b = (Button) findViewById(R.id.button); 
    b.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); 
      intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION | RingtoneManager.TYPE_RINGTONE); 
      intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true); 
      intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); 
      path = load(); 
      Toast.makeText(getBaseContext(),"" + (path == null), Toast.LENGTH_SHORT).show(); 
      startActivityForResult(intent, 1); 
     } 
    }); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (resultCode == RESULT_OK) { 
     Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); 
     if (uri != null) { 
      String ringTonePath = uri.toString(); 
      TextView t = (TextView)findViewById(R.id.textView); 
      t.setText(ringTonePath); 
      save(ringTonePath); 
     } 
    } 
} 

private void save(String ringTonePath) { 

    MyReceiver.sharedPreferences = getPreferences(Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = MyReceiver.sharedPreferences.edit(); 
    editor.putString("song", ringTonePath); 
    editor.putInt("times" , 3); 
    editor.apply(); 
} 
private String load() { 
    MyReceiver.sharedPreferences = getPreferences(Context.MODE_PRIVATE); 
    return MyReceiver.sharedPreferences.getString("song", null); 
} 

}

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
 
    package="com.example.n.receiver" > 
 
    <uses-permission android:name="android.permission.WRITE_SETTINGS"/> 
 
    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/> 
 
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/> 
 
    <application 
 
     android:allowBackup="true" 
 
     android:icon="@mipmap/ic_launcher" 
 
     android:label="@string/app_name" 
 
     android:theme="@style/AppTheme" > 
 
     <activity 
 
      android:name=".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> 
 
     <receiver android:name=".MyReceiver"> 
 
      <intent-filter> 
 
       <action android:name="android.intent.action.ACTION_POWER_CONNECTED" /> 
 
       <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" /> 
 
      </intent-filter> 
 
     </receiver> 
 
    </application> 
 
</manifest>

感謝您的幫助。

+0

請發佈您的清單 –

+0

好的。我更新了我的問題 –

回答

0

正如我懷疑,你有<intent-filter>附加到您的BroadcastReceiver清單。這意味着即使您的應用沒有運行,Android也會實例化您的BroadcastReceiver並致電onReceive()。在這種情況下,MyReceiver中的變量sharedpreferences爲空,因爲它尚未初始化,並且onReceive()會拋出NullPointerException

如果你只是想,當你的應用程序運行這些廣播Intent S,請從清單中<intent-filter>,並使用registerReceiver()而不是代碼註冊BroadcastReceiver

+0

謝謝。是的,這是我的應用中唯一需要的Receiver類。 –

相關問題